我正在通过Learn Java The Hard Way,我被困在这个学习练习中,即使用一个while循环来做与这段代码相同的事情。我想知道你们是否可以帮助我。我的大多数尝试都导致了无限的 while 循环,这是我不想要的。
import java.util.Scanner;
public class RunningTotal
{
public static void main( String[] args)
{
Scanner input = new Scanner(System.in);
int current, total = 0;
System.out.print("Type in a bunch of values and I'll ad them up. ");
System.out.println( "I'll stop when you type a zero." );
do
{
System.out.print(" Value: ");
current = input.nextInt();
int newtotal = current + total;
total = newtotal;
System.out.println("The total so far is: " + total);
}while (current != 0);
System.out.println( "Final total: " + total);
}
}