a. Critically explain why you would consider the concept of using methods in your program again running a couple of if-else statements.
b. Declare a method with three arguments all of the type int. The method should return the greatest of the three arguments, however, if any two or all three are the same, your method should return that value.
c. Embed your method in a complete program that requests for three variables all of the types int and displays the conditions explained in “b” above.
import java.util.Scanner;
/*
The if-else construct is used to choose between two courses of action depending on whether a given condition is true or false.
The if-else statement is always a choice between two alternatives
*/
public class Main {
public static int ThreeNum(int fir, int sec, int the){
int rez;
if(fir > sec){
if(fir>the){
rez = fir;
} else {
rez = the;
}
} else {
if(sec > the){
rez = sec;
} else {
rez = the;
}
}
if(fir == sec & sec == the & fir == the){
rez = fir;
} else {
if(fir == sec)
rez = fir;
if(sec == the)
rez = sec;
if(the == fir)
rez = the;
}
return rez;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println(Main.ThreeNum(5,9,5));
}
}
Comments
Leave a comment