简体   繁体   English

如何在这段代码中使用while循环?

[英]How can I use a while loop in this code?

I am working through Learn Java The Hard Way and I am stuck on this study drill, which is to use a while loop to do the same thing as this code. 我正在通过“学习Java困难的方式”进行工作,并且坚持进行此学习练习,即使用while循环执行与此代码相同的操作。 I was wondering if you guys could help me out. 我想知道你们是否可以帮助我。 Most of my attempts have resulted in an infinite while loop, which is what I do not want. 我的大多数尝试都导致了一个无限的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);

    }
}

A solution that doesn't change all that much code: 不会更改太多代码的解决方案:

int current = -1;

while (current != 0) {
    System.out.print(" Value: ");
    current = input.nextInt();
    int newtotal = current + total;
    total = newtotal; 
    System.out.println("The total so far is: " + total);
}

I don't understand why are you processing(adding to total) when user has entered 0. I know it makes no difference but why unnecessary computations? 我不明白当用户输入0时为什么要进行处理(相加)。我知道这没有区别,但是为什么要进行不必要的计算呢?

Also why define int newtotal in each loop. 还有为什么在每个循环中定义int newtotal You can simply add the sum to the total. 您可以将总和简单地相加。

So the while loop code will go something as follows 所以while循环代码将如下所示

    while((current = input.nextInt()) != 0) {
       total = total + current;
        System.out.println("The total so far is: " + total);
    } 

Turning my comment into an answer: 将我的评论变成答案:

One possible solution: 一种可能的解决方案:

boolean flag = true;
while(flag)
{   
    System.out.print(" Value: ");
    current = input.nextInt();
    int newtotal = current + total;
    total = newtotal; 
    System.out.println("The total so far is: " + total);
    if(current == 0)
        flag = false;
}

Another possible solution: 另一个可能的解决方案:

while(true)
{
    System.out.print(" Value: ");
    current = input.nextInt();
    int newtotal = current + total;
    total = newtotal; 
    System.out.println("The total so far is: " + total);
    if(current == 0)
        break;
}

What about the next: 接下来呢:

Scanner input = new Scanner(System.in);

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." );

int total = 0;
for (int current = -1; current != 0;) {
    System.out.print(" Value: ");
    current = input.nextInt();
    total += current; 
    System.out.println("The total so far is: " + total);
}

System.out.println( "Final total: " + total);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM