打印出最高分

时间:2018-04-21 01:44:57

标签: java methods display

这是我的主要方法,因为没有必要,我不会包含所有其余的代码。但是,我想弄清楚如何打印出三个变量中的最高分: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语句,我不确定什么是最好的方法。

6 个答案:

答案 0 :(得分:0)

我能想到的最简单的方法是使用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);
         }

答案 1 :(得分:0)

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

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

进行操作

答案 2 :(得分:0)

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

my other solution
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以适应任意数量的玩家!

如果你想打印哪个玩家赢了,你可以用这个代替最后一行:

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)));

答案 3 :(得分:0)

在程序中创建一个变量,用于跟踪player()函数中的最高分。例如,

public int position;
int score(int pos)
{
 //input all players score.
 if(Player1> Player2 && Playe1 > Player3)
   return player1;
 else if((Player2> Player3 && Playe2 > Player1)
   return player2;
 else
   return player3;
}

并且在main函数中打印出最高分。

public static void main(String args[])
{
 position = score(0);
 System.out.print("Highest Score is ="+position);
}

或者如果你正在使用像Net-beans这样的IDE,那么通过使用以下行很容易找到它。

IntStream.of(player1, player2, player3).max();

答案 4 :(得分:0)

my other answer相同,但更简单,使用SELECT * FROM your_table WHERE substring(your_column, 1, 1) IN (a, b, c, d) AND substring(your_column, 2, 1) IN (a, b, c, d) AND substring(your_column, 3, 1) IN (a, b, c, d) AND substring(your_column, 4, 1) IN (a, b, c, d); 数组。我们存储了int[]和相应的maxScore,因此我们可以在最后输出它们:

maxUser

答案 5 :(得分:0)

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

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

这里getMarks是你的getter方法,mark是你的变量。

结果result = List.s stream()。最大(COM)获得();

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