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 num1 = reader.readLine();
String num2 = reader.readLine();
String num3 = reader.readLine();
int one = Integer.parseInt(num1);
int two = Integer.parseInt(num2);
int three = Integer.parseInt(num3);
//if num is not greater than x or not less than x print num
if (one <= two && two <= three || three <= two && two <= one) {
System.out.print(two);
} else if (two <= one && one <= three || three <= one && one <= two) {
System.out.print(one);
} else if (one <= three && three <= two){
System.out.print(three);
} else {
System.out.print(three);
}
}
}
How can I make my code better and more efficient and more compact?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Robert
8 July 2019, 19:34
You can declare your variables and initialize them at the same time:
int one = Integer.parseInt(reader.readLine());
int two = Integer.parseInt(reader.readLine());
int three = Integer.parseInt(reader.readLine());
This would eliminate 3 lines.
Once you've done your first two comparisons, no real reason to do the third so you can eliminate your second if/else statement which would eliminate two more lines.
} else if (one <= three && three <= two){
System.out.print(three);
0
karina abad Software Developer at Intuit
8 July 2019, 19:49
Is the logic correct or could it have been done in a better way?
0
Robert
8 July 2019, 21:13
For 3 numbers your logic is spot on in my opinion. If it was asking you to find the middle of 50 numbers, you would first want to sort them, then find the middle. For 3 numbers though that would be overkill and would end up being more lines of code.
0