java在将数组从另一个类接收到方法中而不重新编译时遇到问题
这个问题真的很难解决。我试图寻找类似的情况,但似乎找不到任何有帮助的。我觉得我99%都能做到,这可能很简单,但我一辈子都搞不懂
我有两门课。一流餐厅
问题是,在ClassA的主方法中,我对printRestaurant()的方法调用中,一些方法需要接收带有餐厅名称的数组,但当我使用这些方法时,它会在每次点击下一个方法时对餐厅名称进行重新计算,而不是只提示一次,然后再执行下一个方法
A类:
import java.util.Scanner;
public class ClassA
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String ownerName = "";
System.out.printf("%nWhat is the name of the owner?");
ownerName = input.nextLine();
Restaurant rest = new Restaurant(ownerName);
rest.arraySize();
rest.printRestaurant(ownerName,rest.setRestaurants(),rest.setCustomers(rest.setRestaurants()),rest.calcAvgDailyRev(rest.setRestaurants()));
input.close();
System.exit(0);
}//END main()
}//END Application Class ClassA
餐厅:
public class Restaurant
{
//Fields
String ownerName = "";
int size = 0;
Scanner input = new Scanner(System.in);
public Restaurant()
{
}//END Restaurant()
public Restaurant(String name)
{
ownerName = name;
}//END Restaurant(String ownerName)
public void arraySize()
{
System.out.printf("%n%s, how many restaurants do you own? ",ownerName);
size = input.nextInt();
}//END arraySize()
public String[] setRestaurants()
{
String[] restNames = new String[size];
input.nextLine(); // clears buffer
for(int row = 0;row < restNames.length; row++)
{
System.out.printf("%nEnter restaurant %d",row+1);
restNames[row] = input.nextLine();
}//END for col < restNames.length
return restNames;
}//END setRestaurants()
public int[] setCustomers(String array[])
{
int[] noCustomers = new int[size];
for(int i = 0;i<noCustomers.length;i++)
{
System.out.printf("%nEnter the average number of daily customers for %s: ",array[i]);
noCustomers[i] = input.nextInt();
}//END for i < size
return noCustomers;
}//END setCustomers()
public double[] calcAvgDailyRev(String array[])
{
double[] avgBill = new double[size];
input.nextLine(); //Clears buffer
for(int i = 0;i<avgBill.length;i++)
{
System.out.printf("%nEnter the average bill for %s: ",array[i]);
avgBill[i] = input.nextDouble();
}//end for i < size
return avgBill;
}//END calcAvgDailyRev(String array)
public void printRestaurant(String name, String restName[], int customers[], double dailyAvg[])
{
System.out.printf("%n%n%S's RESTAURANTS",name);
for(int i=0;i<size;i++)
{
double avgRevenue = customers[i]*dailyAvg[i];
System.out.printf("%n%nRestaurant: %s"
+"%nAverage No of Daily Customers: %d"
+"%nAverage Bill Per Customer: $%,.2f"
+"%nAverage Daily Revenue: $%,.2f",restName[i],customers[i],dailyAvg[i],avgRevenue);
}//END for i < size
}//END printRestaurant()
}//END Restaurant
以下是所需输出的示例:
主人叫什么名字?悬崖 克里夫,你有几家餐厅?2. 进入餐厅1:鸡蛋 进入餐厅2:培根 输入鸡蛋的平均每日客户数:250 输入培根的平均每日客户数:200 输入每个客户的鸡蛋平均账单:12 输入每位客户的培根平均账单:15
克利夫餐厅
餐厅:鸡蛋 平均每日客户数:250 每位客户的平均账单:$12.00 平均每日收入:3000美元
餐厅:培根 平均每日客户数:200 每位客户的平均账单:$15.00 平均每日收入:3000美元
如果你需要更多信息,请告诉我。任何帮助都将不胜感激
# 1 楼答案
您需要该方法返回的值。但在这里,您正在调用该方法,并且根据该方法的定义,正在提示输入。 因此,不必一次又一次地调用该方法,您可以保存返回类型并在需要时使用它。 e、 g
您可以使用这些值restName、numberOfCust、dailAvgRev打印餐厅信息
# 2 楼答案
我想问题是你的主方法中没有while循环。因此,nextLine()将只执行一次,而不返回扫描例程。您还可以定义一个特殊字符串,例如“quit”,以指示您要退出扫描过程
试试下面的方法吧D
它会一直提示,直到您输入字符串“退出”。希望有帮助