Java:请问如何在主类使用普通类通过赋值后得到的某个数组,在普通类中返回数组的值好像不可行哎,求解答!谢谢!
普通类代码如下
public class menu {
public double price;
public double getPrice() throws IOException {
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader("menu.txt"));
br.readLine();
String str;
int i=0;
String[] priceArray=new String[]{};
double[] price=new double[4];
while((str=br.readLine())!=null){
priceArray=str.split("\\s+");
for(int j=0;j<3;j++) {
if(j==2) {
price[i]=Double.parseDouble(priceArray[j]);
i++;}
if(i==4) break;
}
}
return price[4];
}
主类相关代码如下:
public static void main(String[] args) throws IOException{
……
if (confirm=='n') {
mu.getPrice();
System.out.println(mu.getPrice());
System.out.println("您今晚的总消费金额为:"+(num1*price[0]+num2*price[1]+num3*price[2]+num4*price[3]));
}
}