3 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
正如這段代碼
while (true) {
永遠(yuǎn)不會(huì)退出,此循環(huán)下面的代碼無(wú)法訪問(wèn)
也許System.exit(0);
應(yīng)該只是打破循環(huán)while
。
break
a中的注釋switch
不會(huì)打破while
循環(huán)

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
將這些行放入 while 循環(huán)中
Collections.sort((themobile, new MobileByBrandName());
System.out.println("Sorted by brand name" + themobile);
Collections.sort(themobile, new MobileByMoNum());
System.out.println("Sorted by brand name" + themobile);
Collections.sort(themobile, new MobileByInS());
System.out.println("Sorted by brand name" + themobile);
Comparator.comparing您還可以使用java 8 及以上版本的可用來(lái)簡(jiǎn)化比較操作。喜歡:
Comparator<Mobile> comparator = Comparator.comparing(new Function<Mobile,
@Override
public String apply(Mobile t) {
return t.getBrandName();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getModelNumber();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getInternalMemoryS();
}
});
Collections.sort(themobile, comparator);

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
while永遠(yuǎn)不會(huì)到達(dá)循環(huán)后的代碼。您需要編寫一些邏輯來(lái)跳出 while 循環(huán)。解決此問(wèn)題的一種簡(jiǎn)單方法是:
int choice = -1;
while (choice != 0) {
choice = input.nextInt();
switch (choice) {
//...other cases
case 0:
System.out.println("Thank you for using a Mobile arraylist");
}
}
Collections.sort(...);
System.out.println(...);
添加回答
舉報(bào)