gpt4 book ai didi

java - 如何保存switch case中输入的数量?

转载 作者:行者123 更新时间:2023-12-01 11:16:30 25 4
gpt4 key购买 nike

我的收银程序遇到问题,我想要求用户输入数量,但数量限制为 4。

我当前的问题是:

我应该如何修复我的程序,以便即使我的命令仍在循环,用户也最多只能输入 4 个。

无论如何,这是我的代码。

public static void main(String[] args) {
Scanner scan = new Scanner(new InputStreamReader(System.in,Charset.defaultCharset()));;
Scanner choiz = new Scanner(System.in);
Scanner pay = new Scanner(System.in);
double total = 0;

double totalA = 0;
double totalB = 0;
double totalC = 0;
double totalD = 0;
double totalE = 0;

double A = 3.25;
double B = 5.00;
double C = 3.00;
double D = 1.50;
double E = 4.50;

char choice;
char choice2;
double payment = 0;
double change = 0;
int numb;
do {
System.out.println("|------------------------------------|");
System.out.println("| MENU |");
System.out.println("|------------------------------------|");
System.out.println("| Press | Product |");
System.out.println("|------------------------------------|");
System.out.println("| A | Product A 3.25 |");
System.out.println("| B | Product B 5.00 |");
System.out.println("| C | Product C 3.00 |");
System.out.println("| D | Product D 1.50 |");
System.out.println("| E | Product E 4.50 |");
System.out.println("|------------------------------------|");
System.out.println("| R | Receipt |");
System.out.println("|------------------------------------|");
System.out.println();
System.out.println("Enter Choice:");

choice = choiz.next().toUpperCase().charAt(0);
switch (choice) {
case 'A':
System.out.println("Item A Selected!");
System.out.println("Please enter number of item/s: ");
//numb = scan.nextInt();
while (!scan.hasNextInt()) {
scan.next();
System.out.print("Please enter an integer: ");
}
numb = scan.nextInt();
if (numb <= 4) {
totalA = numb * A;
System.out.println("Current Total: " + totalA);
}
else {
System.out.println("Only accepting 4 items and below!");
}
break;
case 'B':
System.out.println("Item B Selected!");
System.out.println("Please enter number of item/s: ");
numb = scan.nextInt();
if (numb <= 4) {
totalB = numb*B;
System.out.println("Current Total: " + totalB);
}
else {
System.out.println("Only accepting 4 items and below!");
}
break;
case 'C':
System.out.println("Item C Selected!");
System.out.println("Please enter number of item/s: ");
numb = scan.nextInt();
if (numb <= 4) {
totalC = numb * C;
System.out.println("Current Total: " + totalC);
}
else {
System.out.println("Only accepting 4 items and below!");
}
break;
case 'D':
System.out.println("Item D Selected!");
System.out.println("Please enter number of item/s: ");
numb = scan.nextInt();
if (numb <= 4) {
totalD = numb * D;
System.out.println("Current Total: " + totalD);
}
else {
System.out.println("Only accepting 4 items and below!");
}
break;
case 'E':
System.out.println("Item E Selected!");
System.out.println("Please enter number of item/s: ");
numb = scan.nextInt();
if (numb <= 4) {
totalE = numb * E;
System.out.println("Current Total: " + totalE);
}
else {
System.out.println("Only accepting 4 items and below!");
}
break;
case 'R':
System.out.println("=====================================");
System.out.println("| Receipt for purchase |");
System.out.println("=====================================");
break;
default:
System.out.println("Invalid selection!");
}
}
while (choice != 'R');
total = totalA + totalB + totalC + totalD + totalE;
System.out.println("You purchased the following:");
System.out.println("Item A \t" + "Amount: " + totalA);
System.out.println("Item B \t" + "Amount: " + totalB);
System.out.println("Item C \t" + "Amount: " + totalC);
System.out.println("Item D \t" + "Amount: " + totalD);
System.out.println("Item E \t" + "Amount: " + totalE);
System.out.println("\t\t=====================================");
System.out.println("\t\t| Total bill to pay: " + total + "|");
System.out.println("\t\t=====================================");

System.out.println("Enter your Payment: ");
payment = pay.nextInt();

if(payment > total || payment == total){
change = payment - total;
System.out.println(" Your Payment is " + payment + " and your change is " + change);
System.out.println("[---Thank You for Ordering---]");
}
else{
do{
System.out.println("Insufficient amount of Payment!");
System.out.println("Press [Y] to Try again or [N] to Exit: ");
choice2 = choiz.next().toUpperCase().charAt(0);

switch(choice2)
{
case 'Y': System.out.println("--continue--");
break;

case 'N': System.out.println("--bye--");
Runtime.getRuntime().halt(0);
break;

default:System.out.println("Invalid selection!");
}

}while(choice2!='Y');
System.out.println("Enter your Payment: ");
payment = pay.nextInt();
change = payment - total;
System.out.println(" Your Payment is " + payment + " and your change is " + change);
System.out.println("[---Thank You for Ordering---]");

}}

最佳答案

你可以做这样的事情

首先初始化 4 个变量来跟踪订单,

int A_left = 4;
int B_left = 4;
int C_left = 4;
int D_left = 4;
int E_left = 4;

然后在每个 case 语句中,在获取用户输入的数量后,从适当的变量中减去他订购的数量。

例如对于A

A_left -= numb; // subtract the amount(quantity ordered)

if (A_left < 0) {
System.out.println("You can only choose 4 items max from A");
break;
}

希望这有帮助!

关于java - 如何保存switch case中输入的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com