英文:
display the weight of balls in ascending order
问题
现在信息按顺序显示(按球号)。如何按升序显示球的重量?
public class WorkWithBall {
public static double WeightBall() {
Random random = new Random();
return random.nextInt(10) / 2.5 + 1;
}
}
public class WorkWithBasket {
public static Basket fillBasket(Basket basket) {
for (int i = 0; i < basket.getVolume(); i++) {
Ball temp = new Ball(Color.getRandomColor(), WorkWithBall.WeightBall());
basket.addBall(temp);
}
return basket;
}
public static void InfoOut(Basket basket) {
Arrays.sort(basket.getBasket(), Comparator.comparingDouble(Ball::getWeight)); // 按重量升序排序
for (int i = 0; i < basket.getBasket().length; i++) {
System.out.println("Ball #" + (i + 1) + " with color - " + basket.getBasket()[i].getColor() +
" weight - " + basket.getBasket()[i].getWeight());
}
}
}
英文:
Now the information is displayed in order (by ball number) How can I display the weight of the balls in ascending order?
public class WorkWithBall {
public static double WeightBall(){
Random random = new Random();
return random.nextInt(10)/2.5+1;
}
public class WorkWithBasket {
public static Basket fillBasket(Basket basket){
for(int i =0; i < basket.getVolume(); i++){
Ball temp = new Ball(Color.getRandomColor(),WorkWithBall.WeightBall());
basket.addBall(temp);
}
return basket;
}
public static void InfoOut (Basket basket){
for (int i = 0; i < basket.getBasket().length; i++){
System.out.println("Ball #" + (i+1) + " with color - " + basket.getBasket()[i].getColor()+
" weight - " + basket.getBasket()[i].getWeight());
}
}
答案1
得分: 1
使用Java 8,从balls数组中获取一个流。按照重量排序,如下所示:
Stream.of(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach(ball -> {
System.out.println(ball.getColor() + " " + ball.getWeight());
});
英文:
Using Java 8, get a stream from the balls array. Sort by weight, as follows:
Stream.of(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach(ball -> {
System.out.println(ball.getColor() + " " + ball.getWeight());
});
答案2
得分: 0
你应该使用Stream
和sorted()
,如下所示:
public static void InfoOut(Basket basket) {
Arrays.stream(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach((Ball ball) -> {
System.out.println("球 #" + (i+1) + ",颜色 - " + basket.getBasket()[i].getColor() + ",重量 - " + basket.getBasket()[i].getWeight());
});
}
另外,请注意你的打印语句应该放在从Object
类覆盖的Ball
的toString
方法中。
你还可以简化流的写法,如下所示:
Arrays.stream(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach(System.out::println);
英文:
You should use Stream
and sorted()
like
public static void InfoOut (Basket basket){
Arrays.stream(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach((Ball ball) -> {
System.out.println("Ball #" + (i+1) + " with color - " + basket.getBasket()[i].getColor()+ " weight - " + basket.getBasket()[i].getWeight());
})
}
Also please note that your print should be placed in toString
method of Ball
overriden from Object
class.
And you might simplify the stream like
Arrays.stream(basket.getBasket()).sorted(Comparator.comparing(Ball::getWeight)).forEach(System.out::println);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论