用Java语句,将变量a,b和c中最大的数值赋值给变量d

用Java语句,将变量a,b和c中最大的数值赋值给变量d

import java.util.Scanner;

public class test {
         
    public static void main(String[] args) {
     int a,b,c,d;
     Scanner sc = new Scanner(System.in);
     System.out.println("Please input three numbers:");
     a = sc.nextInt();
     b = sc.nextInt();
     c = sc.nextInt();
     d = maxofthree(a, b, c);
     System.out.println("d = " + d);
     sc.close();
    }

     
    public static int maxofthree(int a,int b,int c){
     int max;
     if(a>b){
     max=a;
     }
     else{
     max=b;
     }
     if(max<c)max=c;
     return max;
    }
     
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2016-12-05
int a=7,b=8,c=1,d=0,temp;
if(a>b){
temp=a;
a=b;
b=temp;
}
if(a>c){
temp=a;
a=c;
c=temp;
}
if(b>c){
temp=b;
b=c;
c=temp;
}
d=c;
System.out.println(d);

第2个回答  2016-12-05
package Test6;
import java.util.*;
public class GGG {
static int a;
static int b;
static int c;
static int d;
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
System.out.println("输入a:");
a=scn.nextInt();
System.out.println("输入b:");
b=scn.nextInt();
System.out.println("输入c:");
c=scn.nextInt();
//法一
// if(a>b&&a>c) {
// d=a;
// }else {
// if(b>c){
// d=b;
// }else if(c>a){
// d=c;
// }
// }
//法二
if(a>b)
d=a;
else
d=b;
if(d>c)
d=d;
else
d=c;

System.out.println("a="+a+"\nb="+b+"\nc="+c+"\nd="+d);
}
}
相似回答