user2984143
user2984143

Reputation: 85

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



       }                        




     }       

  }

Upvotes: 0

Views: 8889

Answers (5)

Jens Piegsa
Jens Piegsa

Reputation: 7485

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

Upvotes: 0

NickJ
NickJ

Reputation: 9559

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

}

Upvotes: 0

Muhammad Kashif Nazar
Muhammad Kashif Nazar

Reputation: 23865

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

 }       

}

Upvotes: 0

Shiliang
Shiliang

Reputation: 61

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

Upvotes: 2

SpiderShlong
SpiderShlong

Reputation: 224

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

Upvotes: 0

Related Questions