从阵列中拉出多个元素并专门标记它们

时间:2015-04-07 03:36:11

标签: java arrays

我需要编写一个在比赛中充当马匹的数组。每当用户按下enter时,马匹就会“跑”,或者阵列会向他们添加随机数并更新他们的位置。每次按下按钮后,控制台都会打印它们的位置。我需要在比赛结束后标记第1和第3匹马(当马匹达到15时)。我无法弄清楚如何做到这一点。

现在我正在搜索数组,如果数组的元素大于或等于15,我将它设置为打印第一名马。如何制作它以便将第2和第3位打印为下一个最高位置(例如,第1 = 15,第2 = 13,第3 = 12)?

此外,似乎我的程序不会停止。达到15或更高后如何让它停止?谢谢!

import java.util.*;
public class HorseRace2{
  public static void main(String[ ] arg){
   Scanner reader = new Scanner(System.in);
   int range = 3;
   int win = 15;
   final int SIZE = 3;
   Random ran = new Random( );
   boolean winner = false; 
   int[ ] arrRan = new int[SIZE];


    System.out.print("Off to the races! Press enter to make the horses run.");

    String readString = reader.nextLine();
    while(readString!=null){
      System.out.print(readString);
      if(readString.equals("")){
         for(int i = 0; i<arrRan.length; i++){
            arrRan[i] = arrRan[i] + (ran.nextInt(3) + 1); 
            System.out.println("Horse " + (i+1) + ":" + arrRan[i]);
         }   

      }//end if

      if(reader.hasNextLine()){
         readString = reader.nextLine();
         System.out.println("Please press enter to make the horses run.");
      }
      for(int i = 0; i<arrRan.length; i++){
         if(arrRan[i]>=win){
            System.out.println("1st place: Horse " + (i+1));
         }
      }
    }//end while


   }//close main
}//close class

2 个答案:

答案 0 :(得分:0)

它没有停止,因为条件readString != null没有被违反。

要打印非第一名匹配马,请对阵列进行排序并根据需要指定位置。

要停止播放,请尝试将while( readString != null )替换为while( nobodyHasWon )。制作一个布尔boolean nobodyHasWon = true并在马获胜时将其设置为假(即System.out.println("1st place: Horse " + (i+1));之后)。

或者,你可以这样做:

// assumptions made: there is only one winner at 15, the rest are below 15, there are no ties, and the first ranked horse has been found
int second, third, secondValue, thirdValue;
secondValue = 0; thirdValue = 0;
for( int i = 0; i < arrRan.length; i++ ) {
    if( i != <indexOfFirstPlaceHorse> )
    {
    if( arrRan[ i ] > secondValue ) {
        second = i;
        secondValue = arrRan[ i ];
    }
    }
}
for( int i = 0; i < arrRan.length; i++ ) {
    if( i != <indexOfFirstPlaceHorse> && i != second ) {
        if( arrRan[ i ] > thirdValue ) {
            third = i;
            thirdValue = arrRan[ i ];
        }
    }
}

答案 1 :(得分:0)

尝试这个我更新了代码。

SEE DEMO

   Scanner reader = new Scanner(System.in);
   int range = 3;
   int win = 15;
   final int SIZE = 3;
   Random ran = new Random( );
   boolean winner = false; 
   int[ ] arrRan = new int[SIZE];


    System.out.print("Off to the races! Press enter to make the horses run.");

    String readString = reader.nextLine();
    while(readString!=null){
        if(!winner){
          System.out.print(readString);
          if(readString.equals("")){
             for(int i = 0; i<arrRan.length; i++){
                arrRan[i] = arrRan[i] + (ran.nextInt(3) + 1); 
                System.out.println("Horse " + (i+1) + ":" + arrRan[i]);
             }   

          }//end if

          for(int j = 0; j<arrRan.length; j++){
             if(arrRan[j]>=win){
                winner =true;
                }
             }


          if(reader.hasNextLine()){
             readString = reader.nextLine();
             System.out.println("Please press enter to make the horses run.");
          }
        }else{
            int[] arrClone = new int[arrRan.length];
            for(int i=0;i<arrRan.length;i++){
                arrClone[i] = arrRan[i];
            }

            arrRan = sortArray(arrRan);
            int[] arrRank = new int[arrRan.length];
            int prevNum = arrRan.length;

            for(int i=0;i<arrRan.length;i++){
                boolean flag = true;
                for(int j=0;j<arrRan.length;j++){
                    if(arrRan[i]==arrClone[j]){
                        if(j!=prevNum && flag){
                                prevNum = j;
                                arrRank[i] = j+1;
                                flag=false;
                        }                                     
                    }                                
                }

            }
            System.out.println("1st place: Horse " + (arrRank[0]));
            System.out.println("2nd place: Horse " + (arrRank[1]));
            System.out.println("3rd place: Horse " + (arrRank[2]));
            break;
        }
    }//end while

    }
    public static int[] sortArray (int[] inArray)
{

    //Construct the array we're using here
    int[] newArray = inArray;

    for(int x = 0; x < newArray.length; x++) //a.length = # of indices in the array
    {

        for(int y = 0; y < newArray.length; y++)
        {
            if(newArray[x] > newArray [y]) {
                int tempValue = newArray[y];
                newArray[y] = newArray[x];
                newArray[x] = tempValue;
            }
        }

    }

    return newArray;
}