I have tried to do this program two different ways. The 1st way I put the numbers in an Array and then pt the array in ascending order. Than I just print the middle number, because the middle number will satisfy all task that are given. If two of the numbers are the same, the middle number will always be one of the two alike number. Also if all the numbers are the same, the middle number will satisfy that task as well. I commented out the array program to try an if else statements.
I also tried to do an if else statement that will satisfy all task. The task that I did not pass are follow:
The program should display the middle number of the three numbers.
If all the numbers are equal, display any one of them.
No matter what I try, the oput is correct by I failed the task
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
jim mather
24 October 2018, 16:19
disregard, i fized the error
0
jim mather
24 October 2018, 15:51
SAme issue and i added BufferedReader. my code is slightly different
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
Integer a = Integer.parseInt(sc.readLine());
Integer b = Integer.parseInt(sc.readLine());
Integer c = Integer.parseInt(sc.readLine());
if ((a == b) || (a == c)) {
System.out.println(a);
} else {
if (b == c) System.out.println(b);
else {
if ((a > b) && (b > c)) System.out.println(b);
else {
if ((b > c) && (c > a)) System.out.println(c);
else {
if ((c > a) && (a > b)) System.out.println(a);
}
}
}
}
}
0
Christopher Wager
24 October 2018, 18:00
Try to read in the line as a String and then convert to an integer.
String input = sc.readLine();
int a = Integer.parseInt();
String input = sc.readLine();
int b = Integer.parseInt();
String input = sc.readLine();
int c = Integer.parseInt();
Hopes that helps!
0
Christopher Wager
21 October 2018, 14:51
Thanks to Khurram suggestion, the problem is resolved. Instead of using Scanner for the input, using BufferedReader for input fixed this problem.
0
Vishwambhar Awagan
21 October 2018, 09:50
You are making it complex Try this
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
if((a>=b && b>=c) || (c>b && b>a))
System.out.println(b);
else if((b>a && a>c) || (c>a && a>b))
System.out.println(a);
else if((c>a && c>b) || (b>c && c>a))
System.out.println(c);
else
System.out.println(c);
0