Answer the question
In order to leave comments, you need to log in
How to display the sum of numbers from the keyboard?
Task with Javarush: Let's write a program in which you need to enter numbers from the keyboard and calculate their sum until the user enters the word "ENTER".
Display the amount received on the screen and exit the program.
I don’t use any methods yet, and for me the solution is quite logical:
Scanner numbers = new Scanner(System.in);
int sum = 0;
int a = numbers.nextInt();
boolean b = false;
while(!b) {
String c = numbers.nextLine();
b = c.equals("ENTER");
System.out.println(sum += a);
}
Scanner scanner = new Scanner(System.in);
int sum = 0;
boolean isExit = false;
while (!isExit) {
String line = scanner.nextLine();
if (line.equals("ENTER")) {
isExit = true;
} else {
int number = Integer.parseInt(line);
sum += number;
}
}
System.out.println(sum);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question