我正在製作一個程序,讓用戶放入他們的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