简体   繁体   中英

Using the while loop to ask user their input in JAVA

I have like 3 hours trying to solve this simple problem. 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.

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.

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
  • initialize total
  • don't initialize input twice
  • 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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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