java 需要循环重复用户输入的次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15601917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:13:02  来源:igfitidea点击:

Need a loop to repeat as many times as the user entered

javafor-loop

提问by TitanC

I need help with this assignment. **Here is what i need the program to do:

我需要帮助完成这项任务。**这是我需要该程序执行的操作:

  • Write a program that prompts a user for a number of iterations.
  • The program should then loop that many times.
  • Each loop the program should prompt the user for a number and add it to a running total.
  • Print the running total when you are done.
  • 编写一个程序,提示用户进行多次迭代。
  • 然后程序应该循环多次。
  • 程序的每个循环都应提示用户输入一个数字并将其添加到运行总数中。
  • 完成后打印运行总计。

I cant get the program to loop as many times as the user enters. I know this is a simple fix but i cant seem to figure it out. Am i using the wrong statement for the loop?

我无法让程序在用户输入时循环多次。我知道这是一个简单的修复,但我似乎无法弄清楚。我在循环中使用了错误的语句吗?

Scanner guess = new Scanner(System.in);

int count =0;
int sum=0;
int num;

System.out.println("Enter a number");
num = guess.nextInt();

for(count =0; count <= num; count++)
{
    sum +=num;
    System.out.println("Your results are:"+sum);
}

回答by jessechk

If you are starting at 0, then you'll want to change

如果你从 0 开始,那么你会想要改变

for(count =0; count <= num; count++)

to

for(count =0; count < num; count++)

because it starts at 0, so that is the first iteration, and 1 is the second, etc.

因为它从 0 开始,所以这是第一次迭代,而 1 是第二次,依此类推。

Also, you need to keep asking the user every time. So you'll want to do something like this in the loop:

此外,您每次都需要不断询问用户。所以你会想要在循环中做这样的事情:

sum += guess.nextInt();

Lastly, you DON'T want to print the sum every time, just at the END. So AFTER the loop, print the sum.

最后,您不想每次都打印总和,只是在 END 时。所以在循环之后,打印总和。

You might have thought that having sum += numwill ask the user for another number, but num is just the first number that the user entered (it won't change). You need to get the user's input every iteration.

您可能认为 havesum += num会要求用户输入另一个数字,但 num 只是用户输入的第一个数字(它不会改变)。您需要在每次迭代中获取用户的输入。

回答by Floris

You are using <=where you should be using just <in you forstatement, otherwise you will go around the loop one to many times; and you should prompt for the number to add inside the loop, and include the appropriate code to keep track of the running sum there.

您正在使用<=您应该<在您的for语句中使用的地方,否则您将循环一到多次;并且您应该提示输入要在循环内添加的数字,并包含适当的代码以跟踪那里的运行总和。

Something like this:

像这样的东西:

Scanner guess = new Scanner(System.in);

int count =0;
int sum=0;
int num, N;

System.out.println("How many numbers will you enter?");
N = guess.nextInt();

for(count =0; count < N; count++)
{
    System.out.println("Enter a number");
    num = guess.nextInt();
    sum +=num;
}

    System.out.println("The sum of the numbers entered is:"+sum);