所以我正在写一个基本的快餐订购程序,我遇到了我的任何一个循环或者返回我的一个变量时出错(所以我认为)。我的代码运行但是当我到达最后并且我不想再输入另一个订单时,它再次询问我然后显示整个食物菜单,然后输出后它允许我结束程序。我将在我认为问题出现的地方发布一半代码。我相信它也必须与userChoice变量有关。
package lab08;
import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;
public class Lab08 {
public static void main(String[] args) throws IOException {
String mealName[];
double mealPrice[];
mealName = new String[3];
mealPrice = new double[3];
LoadArraysFromFile(mealName, mealPrice);
PerformTask(mealName, mealPrice);
}
//Load Arrays From File
public static void LoadArraysFromFile(String mealName[], double
mealPrice[]) throws IOException {
File mealInfoFile;
Scanner mealFileSC;
mealInfoFile = new File("mealInfoFile.txt");
mealFileSC = new Scanner(mealInfoFile);
int cnt = 0;
while (mealFileSC.hasNext() && cnt < 3) {
mealName[cnt] = mealFileSC.nextLine();
mealPrice[cnt] = mealFileSC.nextDouble();
mealFileSC.nextLine();
cnt++;
}
mealFileSC.close();
}
//Process Each Customer
public static void PerformTask(String mealName[], double mealPrice[])
throws IOException {
char userChoice;
userChoice = 'Z';
while (userChoice != 'N') {
userChoice = GetUserChoice();
ProcessEachCustomer(mealName, mealPrice);
}
}
//Get User Choice
public static char GetUserChoice() {
char userChoice;
Scanner kbd = new Scanner(System.in);
System.out.print("Would you like to Order? (Y/N) ");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
while (userChoice != 'Y' && userChoice != 'N') {
System.out.print("Would you like to Order? (Y/N)");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
}
return userChoice;
}
//Process Each Customer
public static void ProcessEachCustomer(String mealName[],
double mealPrice[])throws IOException {
int mealNumber;
double[] mealTotals = new double[3];
int[] quantities = new int[3];
double[] totals;
char userChoice;
userChoice = 'Z';
while (userChoice != 'N') {
ProcessEachMeal(quantities, mealName, mealPrice);
totals = UpdateMealTotals(mealPrice, mealTotals, quantities);
DisplayOrderSummary(mealPrice, mealTotals, quantities, totals,
mealName);
LogTransaction( mealPrice, mealTotals, quantities, totals,
mealName);
userChoice = GetUserChoice();
}
}
//Process Each Meal
public static void ProcessEachMeal(int[] quantities, String
mealName[], double mealPrice[]) {
int mealNumber;
//loop
Scanner kbd = new Scanner(System.in);
DisplayMenu(mealName, mealPrice);
mealNumber = GetMealNumber();
UpdateQuantities(quantities, mealNumber);
}
答案 0 :(得分:2)
在开始GetUserChoice()
之前,然后在处理订单之后,您必须先致电while-loop
。
public static void PerformTask(String mealName[], double mealPrice[]) throws IOException {
char userChoice;
// ask for first time
userChoice = GetUserChoice();
while (userChoice != 'N') {
ProcessEachCustomer(mealName, mealPrice);
// ask again so if user is done (userChoice='N') while-loop will terminate
userChoice = GetUserChoice();
}
}
更新:所有方法都不存在,因此我无法运行完整的代码,但您可以采取以下措施来避免2次打印问题。
下面将确保您在执行任务时仅询问用户选择,而不是在处理每个客户时。
// Process Each Customer
public static void PerformTask(String mealName[], double mealPrice[]) throws IOException {
char userChoice;
userChoice = GetUserChoice();
while (userChoice != 'N') {
ProcessEachCustomer(mealName, mealPrice);
userChoice = GetUserChoice();
}
}
// Get User Choice
public static char GetUserChoice() {
char userChoice;
Scanner kbd = new Scanner(System.in);
do {
System.out.print("Would you like to Order? (Y/N) ");
userChoice = kbd.nextLine().toUpperCase().charAt(0);
} while (userChoice != 'Y' && userChoice != 'N');
return userChoice;
}
// Process Each Customer
public static void ProcessEachCustomer(String mealName[], double mealPrice[]) throws IOException {
int mealNumber;
double[] mealTotals = new double[3];
int[] quantities = new int[3];
double[] totals;
ProcessEachMeal(quantities, mealName, mealPrice);
totals = UpdateMealTotals(mealPrice, mealTotals, quantities);
DisplayOrderSummary(mealPrice, mealTotals, quantities, totals, mealName);
LogTransaction(mealPrice, mealTotals, quantities, totals, mealName);
}