0

I am doing an exercise from a site where I am trying to add 3 numbers the user inputs only using three variables. The code they let you start off with is:

Scanner reader = new Scanner(System.in);
int sum = 0;
int read;

// WRITE YOUR PROGRAM HERE
// USE ONLY THE VARIABLES sum, reader AND read!

System.out.println("Sum: " + sum);

I was thinking of using a for loop but I keep getting stuck because I can only use three variables. reader is already being used by Scanner. sum is going to be changing values, and read is going to be a temporary value.

I would have used

System.out.println("Enter a number: ");
int read = Integer.parseInt(reader.nextLine());

But that would have only held 1 number. Any help I've been stuck on this for a while.

2 Answers 2

2

This should satisfy the requirement:

for(read = 3; read > 0; read--) {
    System.out.println("Enter a number: ");
    sum += Integer.parseInt(reader.nextLine());
}
Sign up to request clarification or add additional context in comments.

4 Comments

Or just sum = reader.nextInt() + reader.nextInt() + reader.nextInt()
Thanks jheim do you mind telling me how you came up with the solution.
Grab my latest edit. I was missing a semicolon, and changed read >= 0 to read > 0.
Well, I tried to incorporate a use of all the variables you had listed. So I decided I would use read as my counter in the for loop. Since we needed to add three values, I set read to 3. The rest of the logic you had already, just incorporated the += operator to add the values to sum. Make sense?
0

Would this do it? It obviously doesn't scale to hundreds of numbers:

int read;
System.out.println("Enter a number: ");
read = Integer.parseInt(reader.nextLine());
sum += read;

System.out.println("Enter a second number: ");
read = Integer.parseInt(reader.nextLine());
sum += read;

System.out.println("Enter a third number: ");
read = Integer.parseInt(reader.nextLine());
sum += read;

No this is what i had as well, but it didn't work

Full version - this works for me.

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int sum = 0;
    int read;

    System.out.println("Enter a number: ");
    read = Integer.parseInt(reader.nextLine());
    sum += read;

    System.out.println("Enter a second number: ");
    read = Integer.parseInt(reader.nextLine());
    sum += read;

    System.out.println("Enter a third number: ");
    read = Integer.parseInt(reader.nextLine());
    sum += read;

    System.out.println("Sum: " + sum);

    }
}

Output:

Enter a number: 
22
Enter a second number: 
10
Enter a third number: 
50
Sum: 82

Of course, a simpler version based on what jheimbouch did would be better:

import java.util.Scanner;

public class HelloWorld 
  {
    public static void main(String[] args) 
    {
    Scanner reader = new Scanner(System.in);
    int sum = 0;

    for(int read = 1; read <= 3; read++) 
      {
      System.out.println("Enter number " + read + ": ");
      sum += Integer.parseInt(reader.nextLine());
      }

    System.out.println("Sum: " + sum);

    }
 }

3 Comments

Doesn't compile? Doesn't add the numbers?
Hmm this is weird I had the same thing as your original one but it would always say read already has a value
Did you maybe have int read = Integer.parseInt(reader.nextLine()); which would mean you had defined read?

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.