CodeGym
Promotion
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
Guanting Liu
Level 16
Springfield
  • 14.10.2020
  • 215views
  • 1comment

what's wrong with this code?

Question about the task Arithmetic mean
Java Syntax,  Level 5,  Lesson 5
Under discussion


Use the keyboard to enter numbers, and then calculate the arithmetic mean.
If the user enters -1, display the arithmetic mean of all entered numbers and end the program.
-1 should not be included in the calculation.

Here are some examples:
a) if you enter the numbers
1
2
2
4
5
-1
then we display
2.8

b) if you enter the numbers
4
3
2
1
-1
then we display
2.5

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 must read data from the keyboard.
  • The program should display data on the screen.
  • After entering -1, the program must correctly terminate.
  • If you sequentially enter the numbers 1, 2, 2, 4, 5 and -1, the program should display 2.8.
  • If you sequentially enter the numbers -100, 0, 100, and -1, the program should display 0.0.
  • If you sequentially enter the numbers 1 and -1, the program should display 1.0.
  • The displayed result should match the task conditions for any input data.
package com.codegym.task.task05.task0507; import java.io.BufferedReader; import java.io.InputStreamReader; /* Arithmetic mean */ public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String number = reader.readLine(); double average = 0; int count = 0; double sum = 0; while(true){ count++; int num =Integer.parseInt(number ) ; if(num == -1) sum+=num ; average = sum/count; System.out.println(average); break; } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nouser
Level 36 , Germany
14 October 2020, 07:18
move line 25 above if (to line 22) and 26,27 below the curly bracket (line 31). Now you enter numbers and have a counter in the loop and all the entered numbers get added to sum. After that you check if the number is -1, if so you break out of the loop. Now you have everything together. A sum, including -1 and a counter. So you calculate the mean and output it.
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.