import java.util.Scanner;
public class CashRegisterSimulation {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int total = 0 , num;
/*
* Create a program that will ask the user to enter a series of amounts.
* Assume that the user is allowed to enter positive numbers only. If the
* user entered a value of 0, the program will stop asking for numbers and
* should display the sum of the inputs given. Use DO WHILE Loops.
*/
do
{
System.out.print("Enter amount : ");
num = console.nextInt();
if(num <= 0)
{
System.out.println("STOP");
total = total + num;
System.out.println("The total amount is : " + total);
}
} while (num > 0);
}
}
如何让我的程序添加用户在输入 0 或负数后输入的所有输入?!
最佳答案
您想要在每次迭代中添加 num > 0
的总和:
do
{
System.out.print("Enter amount : ");
num = console.nextInt();
if(num > 0)
{
total = total + num;
}
} while (num > 0);
System.out.println("STOP");
System.out.println("The total amount is : " + total);
关于java - 如何将用户输入的0或负数后输入的所有内容相加?!使用 do-while 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712877/