package com.codegym.task.task05.task0529;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Console-based piggy bank
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int calcNumbers=0;
while(true){
// Integer.parseInt(reader.readLine());
calcNumbers = calcNumbers += Integer.parseInt(reader.readLine());
if(reader.readLine().equals("sum")){
System.out.println(calcNumbers);
break;
}
}
}
}
Problem with adding
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Gabriella LaPlace
23 August 2019, 15:33
package com.codegym.task.task05.task0529;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Console-based piggy bank
Use the keyboard to enter numbers and calculate their sum until the user enters the word "sum".
Display the resulting sum on the screen.
Hint: to read from the keyboard until the string 'exit' is input, do this:
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
String s = buffer.readLine();
if (s.equals("sum"))
break;
}
Requirements:
1. The program must read data from the keyboard.
2. The program should stop reading data from the keyboard after the user enters the word "sum" in lowercase letters and presses Enter.
3. The program should work correctly if the user enters one number and the word "sum".
4. The program should work correctly if the user enters two numbers and the word "sum".
5. The program should work correctly if the user enters more than two numbers and the word "sum".
6. The program should display text on the screen.
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
int sum = 0;
int number;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
String s = reader.readLine();
if (s.equals("sum"))
break;
number = Integer.parseInt(s);
sum += number;
}
System.out.println(sum);
}
}
+1
GCPix Backend Developer
20 August 2019, 09:42
Could it be this line? calcNumbers = calcNumbers += Integer.parseInt(reader.readLine());
I can't see anything else but not sure how this would only pick up every second input.
0