package com.codegym.task.task04.task0419;
/*
Maximum of four numbers
*/
import java.io.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
Scanner in=new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
int c=in.nextInt();
int d=in.nextInt();
int x=a;
if (a<b){
x=b;
if(b<c){
x=c;
}
else if(b<d){
x=d;
}
}
else if(a<c){
x=c;
if(c<b){
x=b;
}
else if(c<d){
x=d;
}
}
else if(a<d){
x=d;
if(d<b){
x=b;
}
else if(d<c){
x=c;
}
}
System.out.println(x);
}
}
I don't know whats wrong.
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Okanlawon Oluwatobi Damilare
18 March 2019, 20:21
try
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a;
int b;
int c;
int d;
try{
String sa = reader.readLine();
a = Integer.parseInt(sa);
String sb = reader.readLine();
b = Integer.parseInt(sb);
String sc = reader.readLine();
c = Integer.parseInt(sc);
String sd = reader.readLine();
d = Integer.parseInt(sd);
int ma = minimum(a,b);
int mb = minimum(c,d);
if(ma > mb){
System.out.println(ma);
}else{
System.out.println(mb);
}
}catch(IOException e){
e.printStackTrace();
}
}
private static int minimum(int a, int b){
if(a > b){
return a;
}else{
return b;
}
0
Serg
19 April 2019, 12:28
this tasks is about cycles not math methods
and the purpose of it is to learn how to use cycles
0
Guadalupe Gagnon
11 March 2019, 13:56solution
The big problem with your code is the you are doing way too much with your if and else ifs. On top of this, using else-ifs you are stopping execution of the code before everything is evaluated properly. Lets say that you enter the numbers a = 1,b = 2,c = 3, d = 4, stepping through your code would look like this:
At line 20 this would return true because 1 is less than 2
at line 21 x would be set to 2
at line 22 this would return true because 2 is less than 4
at line 23 x would be set to 3
Nothing else would evaluate and line 47 would print 3 even though 4 is the largest value.
I'll message you a solve.
+2