我是新来的
我必须编写一个可以找到放置用户的5个数字中最大的代码。我写了一些东西,但是没有用。谁能帮我?谢谢!
public static void main(String[] args) {
// import java.lang.Math;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 5 integers: ");
int x = Integer.parseInt(keyboard.nextLine());
int y = Integer.parseInt(keyboard.nextLine());
int z = Integer.parseInt(keyboard.nextLine());
int m = Integer.parseInt(keyboard.nextLine());
int n = Integer.parseInt(keyboard.nextLine());
int max = Math.max(x,y);
if (x>y && x>z && x>m && x>n)
System.out.println ("The first of your numbers is the bigest");
else if(y>x && y>z && y>m && y>n)
System.out.println ("The second of your numbers is the bigest");
else if (z>x && z>y && z>m && z>n)
System.out.println ("The third of your numbers is the bigest");
else if (m>x && m>y && m>z && m>n)
System.out.println ("The fourth of your numbers is the bigest");
else if (n>x && n>y && n>z && n>m)
System.out.println ("The fifth of your numbers is the bigest");
System.out.println("The max of three is: " + max);
最佳答案
让Collections
类为您完成它:)
List<Integer> list = Arrays.asList(x,y,z,m,n);
int max = Collections.max(list).intValue();
System.out.println("And the winner is: " + max);
如果还要查找集合中的位置,则应执行以下操作:
int index = list.indexOf(max);
String[]position={"first","second","third","fourth","fifth"};
System.out.println("The "+position[index]+" of your numbers is the bigest");