Java 打印出最高分数

Java 打印出最高分数,java,methods,display,Java,Methods,Display,这是我的主要方法,我不会包括所有其余的代码,因为它不是必需的。然而,我正试图找出如何打印出三个变量中的最高分数:player1,player2,player3 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

这是我的主要方法,我不会包括所有其余的代码,因为它不是必需的。然而,我正试图找出如何打印出三个变量中的最高分数:
player1
player2
player3

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);
List scores=new ArrayList();
scores.put(player1.getScore());
scores.put(player2.getScore());
int highest=Collections.max(arrayList);
System.out.println(“最高分数为:“+最高”);

或者,您可以通过
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));
}
或者如果你想变得(非常)花哨:

与相同,但更简单,使用
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);
}
publicstaticvoidmain(字符串[]args){
System.out.println(“欢迎来到价格正确!”);
int[]分数=新的int[3];
int maxScore=-1;
int-maxUser=0;
for(int i=0;imaxScore){
maxUser=i;
maxScore=分数[i];
}
System.out.println(“玩家”+(i+1)+”您的最终分数是:“+scores[i]);
}
System.out.println(“玩家”+(maxUser+1)+”以“+maxScore”的最终得分获胜);
}

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

首先创建比较器类的对象,如 Comparator com=Comparator.comparing(结果::getMarks)

这里getMarks是getter方法,mark是变量

结果=List.s stream()。Max(com.get()


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

和?它如何处理
if
语句或方法调用?优点和缺点是什么?
intmaxscore=IntStream.of(player1,player2,player3).max()请在发布前在预览部分查看您的帖子,并检查是否一切正常。正如您将看到的,您的代码不正确。同时阅读关于如何写出好答案的文章。更好的答案会让你获得更多的选票,并且被接受的几率更高。
List scores=List.of(player1,player2,player3)漂亮的在线者。另外:
player1
属于
int
类型。您将无法在其上调用
.getScore()
。是的,您是对的,您不能在
int
类型上调用
.getScore()
。但是我刚刚给了她一个想法,将Player视为一个
看看OP试图解决的问题,我认为她没有足够的理解来创建自定义类(但仅凭一个编译器错误就可以推断出这就是你不这么说的意思),谢谢!我正试图将其应用到我的代码中,是否需要初始化链表??无法找到的接收错误symbol@HaleyNewbold如果您的意思是如果需要调用
=newlinkedlist()
:是@HaleyNewbold我建议按原样复制代码,然后按自己的方式返回到自己的解决方案。然后您将看到它在哪一点中断:)@HaleyNewbold您可以像答案中所写的那样调用它:D
List scores=new LinkedList()。但是,如果您想复制此解决方案,请仔细考虑。你的老师会知道这不是你自己的;)您了解阵列了吗?我可以使用ArrayShis快速发布解决方案这帮了我很大的忙!!非常感谢你的时间,为了帮助我明白该怎么做,我非常感谢你的努力。哈利纽博尔德也请考虑所有好的答案,所以将来来到这里的人会更快地找到好的答案:
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)));
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);
}