我的收银机计划已接近完成,它可以处理销售和退货,并在寄存器中添加或减去这笔钱。
我唯一的问题是,一旦我完成了添加值,例如,程序关闭,我无法弄清楚如何将其循环回选择菜单。我尝试使用do循环和do while循环,但它对我说它输入无效(可能是因为你必须在退出时按F才能停止)。
我怎样才能循环这整个事情?
import java.util.Scanner;
import java.util.ArrayList;
public class Assignment3_000848913
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<Integer> Prices = new ArrayList<Integer>();
ArrayList<Integer> ReturnPrices = new ArrayList<Integer>();
int totalRegisterMoney = 0;
int Choice = 0;
System.out.print("What would you like to do?");
System.out.println();
System.out.print("Press 1. Process a sale");
System.out.println();
System.out.print("Press 2. Process a return");
System.out.println();
System.out.print("Press 3. Display Money in register");
System.out.println();
System.out.print("Press 4. Exit");
System.out.println();
Choice = in.nextInt();
if(Choice == 1)
{
//THIS LOOP READS IN ALL THE PRICES//
System.out.print("Press F when finished.");
System.out.println();
do
{
System.out.print("Enter the integer price of the item: $");
int i = in.nextInt();
Prices.add(i);
System.out.println();
}
while(in.hasNextInt());
int totalPrice = processSale(Prices);
totalRegisterMoney = totalRegisterMoney + totalPrice;
System.out.print("Your total comes to $");
System.out.println(totalPrice);
}
if(Choice == 2)
{
System.out.print("Press F when finished.");
System.out.println();
do
{
System.out.print("Enter the price of the returned item: $");
int j = in.nextInt();
ReturnPrices.add(j);
System.out.println();
}
while(in.hasNextInt());
int returnTotal = processReturn(ReturnPrices);
if(returnTotal > totalRegisterMoney)
{
System.out.print("Sorry, there's not that much money in the register.");
System.out.println();
}
else
{
totalRegisterMoney = totalRegisterMoney - returnTotal;
}
System.out.print("You've completed the return.");
System.out.println();
}
if(Choice == 3)
{
viewBalance(totalRegisterMoney);
}
}
//END OF MAIN
答案 0 :(得分:0)
如果你在执行do..while时遇到错误,那么你就可以了。
问题在于写完&#39; F&#39;要退出选项1,循环返回并尝试转换“F&#39; F&#39;进入
Choice = in.nextInt();
这导致错误,因为&#39; F&#39;不是一个数字。 你需要放一个
in.next();
之后
while(in.hasNextInt());
这也会发生在您的代码中的选项2