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
Nitin
Level 5
  • 12.03.2019
  • 1409views
  • 6comments

What is correct way to solve this problem, i tried a lot?

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


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)); String x = reader.readLine(); String y = reader.readLine(); String z = reader.readLine(); int a = Integer.parseInt(x); int b = Integer.parseInt(y); int c = Integer.parseInt(z); if((a > b && c > b) || (b > a && c > a)) { if(a >= b && b <= c) { System.out.println(b); } else if(a >= c && b >= c) { System.out.println(c); } else { System.out.println(a); } } else { System.out.println(c); } } }
0
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
parthasarathy t s
Level 6 , Mumbai, India
18 June 2019, 07:29
just grasp the loic frm below code any dought copy anD PASTE IT
0
parthasarathy t s
Level 6 , Mumbai, India
18 June 2019, 07:28
//write your code here BufferedReader scan=new BufferedReader(new InputStreamReader(System.in)); int a=Integer.parseInt(scan.readLine()); int b=Integer.parseInt(scan.readLine()); int c=Integer.parseInt(scan.readLine()); /* if(a == b || a==c ) { System.out.println(a); } else if(b==c) System.out.println(b);*/ int m1 = 0; if (((a > b) && (a < c)) || ((a > c) && (a < b))) m1 = a; if (((b > a) && (b < c)) || ((b > c) && (b < a))) m1 = b; if (((c > b) && (c < a)) || ((c > a) && (c < b))) m1 = c; if ((a == b) || (a==c)) m1 = a; if (b == c) m1 = b; System.out.println(m1);
0
nikk_shukla
Level 10 , Mumbai, India
16 May 2019, 03:04
Here is my logic:- Suppose three numbers are :- a,b,c. find the largest number between them:-max(max(a,b),max(b,c)). find the lowest number among them:-min(min(a,b),min(b,c)). find the middle number:(a+b+c-largest-lowest)
0
Guadalupe Gagnon
Level 37 , Tampa, United States
16 May 2019, 04:00
That is a good solve, but the max and min should be max(max(a,b),c) and min(min(a,b),c)
+1
Guadalupe Gagnon
Level 37 , Tampa, United States
12 March 2019, 14:22
I personally like to sort the numbers first, then you just need to output the middle one, which would be 'b'. It is easy to do: if(a > b) swap a and b if(b > c) swap b and c if(a > b) swap a and b <--- this is needed again in case and and c swap Sys.out(b) <--- b will satisfy all conditions at this point; if all numebrs are the same, if 2 numbers are the same, or will be the middle number if they are all different. Here is a link with code you can use, look specifically for the "Book's Solution" about midway down: https://codereview.stackexchange.com/questions/38247/inputting-and-sorting-three-integers
+2
Anastasia Falcon
Level 8 , Rio de Janeiro, Brazil
26 April 2019, 15:06
Thanks a lot! At first I didn't get it but then it was all clear.
+1
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.