简体   繁体   English

使用while循环向用户询问他们在JAVA中的输入

[英]Using the while loop to ask user their input in JAVA

I have like 3 hours trying to solve this simple problem. 我有3个小时试图解决这个简单的问题。 Here is what I am trying to accomplished: Ask the user to enter a number, and then add those numbers. 这是我想要完成的:要求用户输入一个数字,然后添加这些数字。 If the users enters five numbers, then I should add five numbers. 如果用户输入五个数字,那么我应该添加五个数字。

Any help will be appreciated. 任何帮助将不胜感激。

  import java.util.Scanner;

  public class loopingnumbersusingwhile
  {
  public static void main(String args[])
  {
       Scanner kb = new Scanner(System.in);  

        int input;         
        System.out.println("How Many Numbers You Want To Enter");
        total = kb.nextInt();
        while(input <= kb.nextInt()) 
     {
         input++;

        System.out.println("How Many Numbers You Want To Enter" + input);
        int input = kb.nextInt();



       }                        




     }       

  }

Your current code is trying to use input for too many purposes: The current number entered, the amount of numbers of entered, and is also trying to use total as both the sum of all numbers entered and the amount of numbers to be entered. 您当前的代码尝试将input用于太多目的:输入的当前数字,输入的数量,并且还尝试使用total作为输入的所有数字的总和和要输入的数字的数量。

You'll want 4 separate variables to track these 4 separate values: how many numbers the user will entered, how many they entered so far, the current number they entered, and the total. 您需要4个单独的变量来跟踪这4个单独的值:用户输入的数量,到目前为止输入的数量,输入的当前数量以及总数。

int total = 0; // The sum of all the numbers
System.out.println("How Many Numbers You Want To Enter");
int count = kb.nextInt(); // The amount of numbers that will be entered
for(int entered = 0; entered < count; total++)
{
    int input = kb.nextInt(); // the current number inputted
    total += input; // add that number to the sum
}
System.out.println("Total: " + total); // print out the sum

Add this code after you take how many numbers the user wants to add: 在获取用户想要添加的数量后添加此代码:

int total;
for(int i = 0; i < input; i--)
{
    System.out.println("Type number: " + i);
    int input = kb.nextInt();
    total += input;
}

To print this just say: 打印这个只是说:

System.out.println(total);
import java.util.Scanner;

public class LoopingNumbersUsingWhile
{
  public static void main(String args[])
  {
   Scanner kb = new Scanner(System.in);  

    int input=0;

    int total = 0;

    System.out.println("How Many Numbers You Want To Enter");
    int totalNumberOfInputs = kb.nextInt();

    while(input < totalNumberOfInputs) 
    {
      input++;

      total += kb.nextInt();

    }

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

 }       

}

What you should pay attention to: 你应该注意什么:

  • name classes in CamelCase starting with a big letter CamelCase中的名字类,以大写字母开头
  • initialize total 初始化total
  • don't initialize input twice 不要初始化input两次
  • show an appropriate operand input request to your user 向用户显示适当的操作数输入请求
  • take care of your loop condition 照顾你的循环条件
  • don't use one variable for different purposes 不要将一个变量用于不同的目的
    • which variable should hold your result? 哪个变量应该保留你的结果?
  • how to do the actual calculation 怎么做实际计算

Possible solution: 可能的方法:

import java.util.Scanner;

public class LoopingNumbersUsingWhile {
    public static void main(String args[]) {
        Scanner kb = new Scanner(System.in);

        System.out.println("How Many Numbers You Want To Enter: ");
        int total = kb.nextInt();
        int input = 0;
        int sum = 0;
        while (input < total) {
            input++;

            System.out.println("Enter " + input + ". Operand: ");
            sum += kb.nextInt();
        }
        System.out.println("The sum is " + sum + ".");
    }
}

You seem to be asking how many numbers twice. 你似乎要问两次有多少个数字。

public static void main(String args[])
{
 Scanner kb = new Scanner(System.in);  

 System.out.println("How Many Numbers You Want To Enter");
 int howMany = kb.nextInt();
 int total = 0;

 for (int i=1; i<=howMany; i++) {
   System.out.println("Enter a number:");
   total += kb.nextInt();
 }

 System.out.println("And the grand total is "+total);

} }

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

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