我正在尝试编写一个提示用户输入三个数字的代码,并且程序应该说哪个数字是最大的。我不想在 if 和 else if 语句中做一堆“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);
}
}