0

UPDATE: Figured it out, code has been fixed below. Added a while loop to confirm values 0 or above are entered too.

So I'm doing an assignment where the user enters 8 scores and you must find the highest and lowest scores and their position in the order they were given. I have been able to find the highest and lowest scores but I can't figure out how to find their position. Please help me. Here is my code so far.

import java.util.Scanner;
public class HighestLowestPoints {
    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    int largest = -1;
    int smallest = 0;
    int highGame = 1;
    int lowGame = 1;

    for(int games = 1; games <= 8; games++) {
        System.out.println("Please enter the Texans' score in game " + games);
        int points = keyboard.nextInt();
        while(points < 0) {
            System.out.println("Please enter a value 0 or above");
            points = keyboard.nextInt();
        }
        if(games == 1) {
            smallest = points;
        }
        if(points > largest) {
            largest = points;
            highGame = games;
        }
        if(points < smallest) {
            smallest = points;
            lowGame = games;
        }
        if(games == 8){
            System.out.println(largest + " in game " + highGame + "\n" 
                              + smallest + " in game " + lowGame);
        }

    }
  }
}
4

7 に答える 7

1

あなたの位置を含む for ループでゲームカウンター変数を使用できます:-)

于 2013-10-07T20:34:53.390 に答える
1

もうすぐそこです。変数に追加して、hightGame と lowerGame を保存し、それぞれ最高スコアと最低スコアを設定するときにそれらを割り当てる必要があります。

元:

    if(points < smallest) {
        smallest = points;
        lowestGame = games;
    }
于 2013-10-07T20:38:17.510 に答える
0

ご参考まで。また、これらすべての異なる if を回避し、すべてのゲームをArrayList. そして、メソッドCollections.maxを使用して最大ゲームCollections.minを取得し、最小ゲームを取得します。次に、メソッドlist.indexOfを使用して、値がリストに追加される位置を見つけることができます。

Scanner keyboard = new Scanner(System.in);
List<Integer> numbers = new ArrayList<Integer>();
for(int games = 1; games <= 8; games++) {
    System.out.println("Please enter the Texans' score in game " + games);
    int points = keyboard.nextInt();
    while(points < 0) {
        System.out.println("Please enter a value 0 or above");
    points = keyboard.nextInt();
    }
    numbers.add(points);
}
StringBuilder output = new StringBuilder();
output.append("Maximum points: " + Collections.max(numbers));
output.append(" in game: " + (numbers.indexOf(Collections.max(numbers)) + 1));
output.append(" minimum points: " + Collections.min(numbers));
output.append(" in game: " + (numbers.indexOf(Collections.min(numbers)) + 1));
System.out.println(output.toString());
于 2013-10-07T21:21:39.480 に答える
0

別の 2 つの変数 maxIndex と minIndex を追加し、最小値または最大値を設定する場合は、適切な変数と "games" 変数 (位置を含む) を設定します。

于 2013-10-07T20:36:32.063 に答える
0

2 つの新しい変数を作成します。

int highPos = -1; int lowPos = -1;

最小を割り当てるたびに、ゲームを lowPos に割り当てます。最高を割り当てるたびに、ゲームを highPos に割り当てます。

于 2013-10-07T20:36:40.560 に答える
0

追加

int lPos = 0, sPos = 0;

あなたのforループの前に。

lPos = gamesこれを最大数を見つけsPos = gamesたifブロックと最小数のifブロックに追加します。

于 2013-10-07T20:37:21.453 に答える
0

また、最初のゲームに最低を割り当て、最高を割り当てないのはなぜですか?

if(games == 1) {
    smallest = points;
}
于 2013-10-07T20:42:32.297 に答える