显示球的重量按升序排列

huangapple go评论68阅读模式
英文:

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 &lt; 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 &lt; basket.getBasket().length; i++){
        System.out.println(&quot;Ball #&quot; + (i+1) + &quot; with color - &quot; + basket.getBasket()[i].getColor()+
                &quot; weight - &quot; + 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 -&gt; {
			System.out.println(ball.getColor() + &quot; &quot; + ball.getWeight());
		});

答案2

得分: 0

你应该使用Streamsorted(),如下所示:

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类覆盖的BalltoString方法中。
你还可以简化流的写法,如下所示:

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) -&gt; {
        System.out.println(&quot;Ball #&quot; + (i+1) + &quot; with color - &quot; + basket.getBasket()[i].getColor()+ &quot; weight - &quot; + 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);

huangapple
  • 本文由 发表于 2020年9月28日 14:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64097227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定