我应该要求用户输入一个百吉饼代码,询问他们是否需要更多,并且应该输出一个输出文件,但这一切都不会发生,唯一发生的事情就是printMenu方法。 / p>
这是我的代码:
import java.io.*;
import java.util.*;
public class Lab5
{//start of class
public static void main(String [] args) throws FileNotFoundException
{//start of main
Scanner keyIn = new Scanner(System.in);
// assigns "keyIn" to keyboardScanner
Scanner bagelFile = new Scanner(new FileReader("Lab5Input.txt"));
//open the input file
//Data Dictionary - declare variables
int sub;
int bagelCode;
int bagelQuantity;
int moreBagels=1;
double totalCost; //75 cents per bagel
int totalQuantity=0;
boolean inputBad = true;
//create arrays for description, inventory
String [ ] descArray = new String [13];
int [ ] invArray = new int [13];
//Load Bagel arrays from input file - use a while loop
sub = 0;
while(bagelFile.hasNext())
{
descArray[sub] = bagelFile.next();
invArray[sub] = bagelFile.nextInt();
sub ++;
}
// start a customer
System.out.println("Welcome to Dan's Bagel House, here's our menu: ");
System.out.println("");
System.out.println("");
// print menu
printMenu (descArray, invArray, 13);
while (moreBagels==1); // moreBagels = 1
{//begin while loop for one customer
totalCost = 0;
//verify bagel code
do
{
System.out.print ("\nEnter bagel # ");
bagelCode = keyIn.nextInt();
if (bagelCode >=1 && bagelCode<= 13)
inputBad= false;
}//end of loop
while (inputBad);
sub = bagelCode - 1;
// verify quantity
inputBad = true;
do
{
System.out.print("Please enter quantity of bagels");
bagelQuantity = keyIn.nextInt ( );
if (bagelQuantity >= 0 && bagelQuantity <= invArray[sub])
inputBad = false;
}
while (inputBad);
totalQuantity = totalQuantity + bagelQuantity;
invArray[sub] = invArray[sub] - bagelQuantity;
//one bagel purchase complete
//do you want more bagels 1 or 0
inputBad = true;
do
{
System.out.print ("Do you want additional bagels? Enter 1 for Yes or 0 for No");
moreBagels = keyIn.nextInt( );
if (moreBagels == 1 || moreBagels ==2)
inputBad = false;
}
while (inputBad);
}//end while loop for one customer
totalCost = totalQuantity * .75;
//end of a customer
System.out.print("Thank you for your purchase");
printReport (descArray, invArray, 13);
}//end of main
///////////////////////////////////////////////printMenu Method///////////////////////////////////////////////////////
public static void printMenu (String [ ] mDescArray, int [ ] mInvArray, int maxIndex)
{
System.out.println("Bagel Code Description Current Inventory ");
int mSub;
for (mSub = 0; mSub<13; mSub++)
{
int m = mSub+1;
System.out.printf("%6d",m);
System.out.printf(" %20s",mDescArray[mSub]);
System.out.printf("%15d%n",mInvArray[mSub]);
}
}//end of main
///////////////////////////////////////////////printReport Method///////////////////////////////////////////////////////
public static void printReport (String [ ] mDescArray, int [ ] mInvArray, int maxIndex) throws FileNotFoundException
{//declare output file here
PrintWriter outBagelFile = new PrintWriter("Lab5Output.txt");
outBagelFile.println ("Bagel Code Description Current Inventory ");
int mSub;
for (mSub = 0; mSub<13; mSub++);
{
int m = mSub+1;
outBagelFile.printf("%4d",m);
outBagelFile.printf(" %-15s",mDescArray[mSub]);
outBagelFile.printf("%4d%n",mInvArray[mSub]);
}
outBagelFile.close( );
}//end of main
}//end of class
这是输出:
Welcome to Dan's Bagel House, here's our menu:
Bagel Code Description Current Inventory
1 asiagocheese 12
2 blueberry 12
3 cheddarcheese 6
4 everything 12
5 garlic 12
6 honey 4
7 jalepeno 3
8 multigrain 12
9 onion 12
10 poppyseed 12
11 raisin 6
12 sesame 12
13 wholewheat 12
答案 0 :(得分:0)
如果数量不足,我建议打印一条消息:
if (bagelQuantity >= 0 && bagelQuantity <= invArray[sub])
inputBad = false;
else
System.out.print("Not enough quantity");
此外,在printMenu和printReport中使用数组长度或maxIndex,而不是硬编码数字13,并在printReport中删除for(&#34 ;;&#34;)后的分号:
for (mSub = 0; mSub < maxIndex; mSub++)
错误显然是在第一次,而且有#34 ;;&#34;在它的最后,删除它:
while (moreBagels == 1) // moreBagels = 1
此外,当要求更多百吉饼时检查0,而不是2:
if (moreBagels == 1 || moreBagels == 0)
inputBad = false;
最后放轻松!