0

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



       }                        




     }       

  }

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

 }       

}

Comments

0

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

}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.