打印最高分

Print out the highest score

这是我的主要方法,我不会包含所有其余代码,因为没有必要。但是,我想弄清楚如何打印出三个变量中的最高分:player1player2player3

public static void main(String[] args) {
    System.out.println("Welcome to Price is Right!");

    System.out.println("Player 1 are you ready to spin?");
    int player1 = player();
    System.out.println("Player 1 Your final score is: " + player1);

    System.out.println("Player 2 are you ready to spin?");
    int player2 = player();
    System.out.println("Player 2 Your final score is: " + player2);

    System.out.println("Player 3 are you ready to spin?");
    int player3 = player();
    System.out.println("Player 3 Your final score is: " + player3);

    // todo..
}

如有任何想法,我们将不胜感激。我想过调用一个方法来计算这个或使用 if 语句,我不确定什么是最好的方法。

我能想到的最简单的方法是使用 if 和 else 语句:

  if(Player1> Player2 && Playe1 > Player3){
        System.out.println("Highest score was earned by Player 1 which is"+ player1);
  }else if(Player2> Player3 && Playe2 > Player1){
        System.out.println("Highest score was earned by Player 2 which is"+ player2);  
  }else{
        System.out.println("Highest score was earned by Player 3 which is"+ player3);
         }

你可以把所有玩家的分数放在一个数组中,然后找出其中最高的

List<Integer> scores = new ArrayList<>();
scores.put(player1.getScore());
scores.put(player2.getScore());
int highest = Collections.max(arrayList);
System.out.println("Highest score is: " + highest);

或者您可以通过 IF-Else

这个非常紧凑且易于扩展的解决方案怎么样?你的老师会感到骄傲!如果太复杂,请查看 ,它仅使用 int[]

public static void main(String[] args) {
    System.out.println("Welcome to Price is Right!");
    int playerCount = 3;
    List<Integer> scores = new LinkedList<>();
    for (int i = 0; i < playerCount; i++) {
        System.out.println("Player " + (i+1) + " are you ready to spin?");
        scores.add(player());
        System.out.println("Player " + (i+1) + " Your final score is: " + scores.get(scores.size()-1));
    }

    System.out.println("The maximum score is " + Collections.max(scores));
}

我们没有将每个玩家的分数存储在自己的变量中,而是将它们全部收集在一个 Collection 中。通过这种方式,我们可以简单地更改 playerCount 以容纳任意数量的玩家!

如果你想打印 which player won 你可以用这个替换最后一行:

int maxScore = Collections.max(scores);

for (int i = 0; i < scores.size(); i++) {
    if (scores.get(i) == maxScore) {
        System.out.println("Player " + (i+1) + " won with a final score of " + maxScore);
        break;
    }
}

或者如果你想(非常)花哨:

OptionalInt maxVal = IntStream.range(0, playerCount).reduce((a, b) -> scores.get(a) > scores.get(b) ? a : b);
maxVal.ifPresent(i -> System.out.println("player " + (i+1) + " won with a final score of " + scores.get(i)));

相同,但更简单,使用int[]数组。我们边走边存maxScore和对应的maxUser,所以我们可以在最后输出:

public static void main(String[] args) {
    System.out.println("Welcome to Price is Right!");
    int[] scores = new int[3];
    int maxScore = -1;
    int maxUser = 0;
    for (int i = 0; i < scores.length; i++) {
        System.out.println("Player " + (i+1) + " are you ready to spin?");
        scores[i] = player();
        if (scores[i] > maxScore) {
            maxUser = i;
            maxScore = scores[i];
        }
        System.out.println("Player " + (i+1) + " Your final score is: " + scores[i]);
    }
    System.out.println("Player " + (maxUser+1) + " won with a final score of " + maxScore);
}

考虑到结果是您的自定义对象,它包含标记。

首先创建比较器的对象class like 比较器 com= comparator.comparing(Result::getMarks);

此处 getMarks 是您的 getter 方法,而 mark 是您的变量。

结果结果=List.sstream()。 Max(com).get();

您也可以使用 min 函数找到最小值..