3 回答
TA贡献1797条经验 获得超6个赞
正如这段代码
while (true) {
永远不会退出,此循环下面的代码无法访问
也许System.exit(0);
应该只是打破循环while
。
break
a中的注释switch
不会打破while
循环
TA贡献1829条经验 获得超7个赞
将这些行放入 while 循环中
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 及以上版本的可用来简化比较操作。喜欢:
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贡献1936条经验 获得超6个赞
while永远不会到达循环后的代码。您需要编写一些逻辑来跳出 while 循环。解决此问题的一种简单方法是:
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(...);
添加回答
举报