我正在制作一个程序,让用户放入他们的Class ID,然后输入销售的盒子数量。我试图让计算机最终告诉我十个班中哪些班最多。但我似乎无法弄清楚如何让程序告诉我十个中哪一个盒子最多。 我想我需要找到数组中最大的数字,如果这甚至可能?java中数组最大的数字?
import java.util.*;
public class Boxs {
int ID, boxs;
public static void main(String[] args) {
int p = 0;
Scanner scan = new Scanner(System.in);
Boxs[] bx = new Boxs[10];
for (int i = 0; i <= 9; i++) {
bx[i] = new Boxs();
System.out.print("Enter Class ID: ");
bx[i].ID = scan.nextInt();
System.out.print("Enter boxs sold: ");
bx[i].boxs = scan.nextInt();
}
int temp = 0;
int temp2 = 0;
for (int j = 0; j < 9; j++) {
for (int h = 0; h < 9; h++) {
if (bx[h].boxs > bx[h+1].boxs) {
temp2 = bx[p].boxs;
bx[h].boxs = bx[p+1].boxs;
bx[p+1].boxs = temp;
temp = bx[h].ID;
bx[h].ID = bx[p+1].ID;
bx[h+1].ID = temp2;
System.out.println(bx[h].boxs);
System.out.println(bx[h+1].boxs);
}
}
}
System.out.println("The Class ID with the most boxes is: " + bx[0].ID + " and sold " + bx[0].boxs + " boxs.");
}
}
可能重复:HTTP://stackoverflow.com/questions/1484347/java-max-min-value-in-an-array – tenorsax 2012-01-29 00:28:12