Hey I am new to Java programing and I am wrestling with changing varibles and looking for a bit of help. The question is below:
Create a program that asks the user for three numbers and then prints their sum. Use the following structure in your program:
// TODO code application logic here
Scanner reader = new Scanner(System.in);
int sum = 0;
int read;//`enter code here`
// WRITE YOUR PROGRAM HERE
// USE ONLY THE VARIABLES sum, reader AND read!
System.out.println("Sum: " + sum);
This is what I wrote below and of course I am getting syntax errors:
Scanner reader = new Scanner(System.in);
int sum = 0;
System.out.print("Type the first Number: ");
int read = Integer.parseInt(reader.nextLine());
System.out.print("Type the second number: ");
int read = read + (Integer.parseInt(reader.nextLine()));
System.out.print("Type the third number: ");
int read = read + (Integer.parseInt(reader.nextLine()));
sum = read;
System.out.println("Sum: " + sum);
int readis only required to create the variable. After that you can just useread. Also it's probably better to accumulate the result in sum instead of in read, that's it's purpose after all