ユーザーに3つの数字の入力を求めるコードを書き込もうとしていますが、プログラムはどの数字が最大かを示すことになっています。ifステートメントとelseifステートメントで大量の「System.out.print」を実行したくありませんでした。デバッガによるエラーは、「greatest」と「greatest1」が初期化されていないことです。
import java.util.Scanner;
public class number1
{
public static void main(String[] args)
{
double a, b, c;
double greatest, greatest1;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter one number :");
a = keyboard.nextDouble();
System.out.print("Enter another number :");
b = keyboard.nextDouble();
System.out.print("Enter a third number :");
c = keyboard.nextDouble();
if(a > b && a > c) {
greatest = a;
} //end of if statement
else if(b > a && b > c){
greatest = b;
}
else if(c > a && c > b) {
greatest = c;
}
else if(a==b && c < a) {
greatest = a;
greatest1 = b;
}
else if(a==c && b < a) {
greatest = a;
greatest1 = c;
}
else if(b==c && a < b) {
greatest = b;
greatest1 = c;
}
else {
System.out.print("All of the numbers are greatest");
}
System.out.print("The greatest number is: " +greatest+ "and" +greatest1);
}
}