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
Nakul Sehgal
Level 9
New Delhi
  • 10.04.2020
  • 610views
  • 2comments

Dont understand what's wrong with this code?

Question about the task Somehow average
Java Syntax,  Level 4,  Lesson 16
Resolved


Use the keyboard to enter three numbers, and then display the middle number.
In other words, not the largest and not the smallest.
If all the numbers are equal, display any one of them.

Requirements:
  • The program should read the numbers from the keyboard.
  • The program must display a number on the screen.
  • The program should display the middle number of the three numbers.
  • If all the numbers are equal, display any one of them.
  • If two of the three numbers are equal, display either of the two.
package com.codegym.task.task04.task0441; /* Somehow average */ 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 a = Integer.parseInt(reader.readLine()); int b = Integer.parseInt(reader.readLine()); int c = Integer.parseInt(reader.readLine()); if(a==b&&b==c) System.out.println(b); if(a>b&&a>c){ if(b>c) System.out.println(b); else System.out.println(c); } if(b>a&&b>c){ if(a>c) System.out.println(a); else System.out.println(c); } if(c>a&&c>b){ if(a>b) System.out.println(a); else System.out.println(b); } if(a==b&&a!=c) System.out.println(a); if(a!=b&&b==c) System.out.println(b); if(a==c&&a!=b) System.out.println(c); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gellert Varga
Level 23 , Szekesfehervar, Hungary
10 April 2020, 23:27
Try out your program with this three numbers: 2 2 8 and it will give this output: 2 2. Two of the three numbers are equal, and the program displays both of them! But should display only one. Line 31: true Line 32: false therefore the "else" thread is executed: Line 35: prints "b" (=2). Line 37 is also true, therefore prints again a variable: "a" (=2).
0
Nakul Sehgal
Level 9 , New Delhi, India
12 April 2020, 09:26
Thanks. Got 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.