实现从键盘输入三个整数,判断三个整数中的最大数,并输出。
Scanner sc = new Scanner(System.in);
System.out.print(“请输入第一个数字:”); int a = sc.nextInt();
System.out.print(“请输入第二个数字:”); int b = sc.nextInt();
System.out.print(“请输入第三个数字:”); int c = sc.nextInt();
//方法一用if else实现
int max = a;
if(max<b) {
max = b;
System.out.print("三个数中最大数为:"+max); }else if(max<c){
max=c;
System.out.print("三个数中最大数为:"+max); }else {
System.out.print("三个数中最大数为:"+max); }
//方法二 用三目实现
int temp = a>b?a:b;
int max1 = temp>c?temp:c;
System.out.println("三个数中最大数为:"+max1);
//方法三 用数组实现
int [] arry = {a,b,c};
int max2=arry[0];
for (int i=0;i<arry.length;i++) {
if(max2<arry[i]) {
max2=arry[i];
}
}
System.out.println("三个数中最大数为:"+max2); }