【发布时间】:2018-04-25 13:27:39
【问题描述】:
我的结果要我:当我在(输入产品编号)中输入 -1 时,它将直接打破并计算总数,但现在我必须在(输入产品编号)和(输入产品编号)中输入 -1数量)只有它会打破
import java.util.Scanner;
public class Structuredcontrols {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
double sum=0;
double sum2=0;
double sum3=0;
int number = 0;
int quantity;
while (number != -1) {
System.out.print("Enter product number: ");
number = input.nextInt();
System.out.print("Enter quantity number: ");
quantity = input.nextInt();
switch (number) {
case 1:
sum += 2.98 * quantity;
break;
case 2:
sum2 += 4.50 * quantity;
break;
case 3:
sum3 += 9.98 * quantity;
break;
}
}
System.out.println("Total value of product 1 is: "+sum);
System.out.println("Total value of product 2 is: "+sum2);
System.out.println("Total value of product 3 is: "+sum3);
}
}
【问题讨论】:
-
格式错误的代码很难阅读。尝试始终如一地进行换行和缩进。无论哪种方式,解决方案应该是添加 if(number == -1) { break; } 在“输入数量”之前
-
使用“if”语句...
-
好的,谢谢你的帮助,顺便说一句,我不知道如何用正确的格式编写代码,因为我将近 1 年没有向 stackoverflow 提问(我会向其他提问者学习)。跨度>
标签: java