CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
niki4etoo
Level 8
Ruse
  • 04.09.2018
  • 1450views
  • 2comments

Why I did not match the last requirement?

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

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.*; /* Arithmetic mean */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); double result = 0.0f; int sum = 0; int i = 0; while(true) { String numbers = br.readLine(); int num = Integer.parseInt(numbers); if(num == -1) { break; } if(num != 0) { sum += num; i++; } } if(sum == 0) { result = 0.0; } else { result = (double)sum/(double)i; } System.out.print(result); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
niki4etoo
Level 8 , Ruse, Bulgaria
6 September 2018, 20:15
I figure it out. Thanks. It doesn't need to exclude the zero.
0
Roman
Level 41
6 September 2018, 06:10solution
if(num != 0)
why?
+3
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.