CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Nitin
Level 5
  • 12.03.2019
  • 1082views
  • 5comments

Help!

Question about the task Adding
Java Syntax,  Level 4,  Lesson 16
Under discussion


Use the keyboard to enter numbers.
If the user enters -1, display the sum of all entered numbers and end the program.
-1 should be included in the sum.

Hint: one of the solutions to this problem uses the following construct:

while (true) {
    int number = read the number;
    if (check whether the number is -1)
        break;
}

Requirements:
  • The program should read the numbers from the keyboard.
  • If the user enters -1, the program should display the sum of all entered numbers and then end.
  • The program must calculate and display the sum of the entered numbers.
  • The program must use a for loop or a while loop.
package com.codegym.task.task04.task0442; /* Adding */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //int number = 0; int total = 0; while(true) { int a = Integer.parseInt(reader.readLine()); if(a == -1) { total += a; System.out.println(total); break; } //System.out.println(total); } } }
0
Comments (5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jeff Jeffy
Level 15 , Irvine, United States
19 March 2019, 04:28
All things wrong: (1) int inside of a while loop may cause issues due to repeatedly declaring variables. I suggest you delcare them like this
int total=0,a=0;
(2)
if(a == -1)
            {
                total += a;
                System.out.println(total);
                break;
            }
in that section, you force the use to type -1 in order to add. This is the complete opposite of what you need. (3) Uncomment line 27.
//System.out.println(total);
You need to to fulfill task conditions.
0
Mrunali Punde
Level 6 , Mumbai, India
16 March 2019, 06:39
write the total+=a line before the if block. and print the total after the if block.
0
Nastya Varennikova
Level 11 , Sumy, Ukraine
12 March 2019, 22:01
System.out.println(total-1);
//in 27-th lines
0
Guadalupe Gagnon
Level 37 , Tampa, United States
12 March 2019, 14:01
The code doesn't add anything to total except -1. You could enter millions of numbers, but the output would be -1 because line 23 is the only line that adds anything to 'total', and that is in the if block that will only be run if 'a' is equal to -1. Try moving line 23 to before the if block.
0
Kholmamat
Level 12 , Uzbekistan Navoi, Uzbekistan
12 March 2019, 12:07
U hava to print result after cycle.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.