繁体   English   中英

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

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

我有3个小时试图解决这个简单的问题。 这是我想要完成的:要求用户输入一个数字,然后添加这些数字。 如果用户输入五个数字,那么我应该添加五个数字。

任何帮助将不胜感激。

  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();



       }                        




     }       

  }

您当前的代码尝试将input用于太多目的:输入的当前数字,输入的数量,并且还尝试使用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

在获取用户想要添加的数量后添加此代码:

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

打印这个只是说:

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

 }       

}

你应该注意什么:

  • CamelCase中的名字类,以大写字母开头
  • 初始化total
  • 不要初始化input两次
  • 向用户显示适当的操作数输入请求
  • 照顾你的循环条件
  • 不要将一个变量用于不同的目的
    • 哪个变量应该保留你的结果?
  • 怎么做实际计算

可能的方法:

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

你似乎要问两次有多少个数字。

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