数组

时间:2016-03-07 13:52:08

标签: java arrays

下面有一个程序从2个输入文件中读取数据,随机选择比赛,模拟比赛资格,比赛并打印出结果。

这是它的两个文件:

drivers.text:

Mercedes,Lewis Hamilton
Mercedes,Nico Rosberg
Ferrari,Sebastian Vettel
Ferrari,Kimi Raikkonnen
Williams,Felipe Massa
Williams,Valtteri Bottas
Red Bull Racing,Daniel Ricciardo
Red Bull Racing,Daniil Kvyat
Force India,Sergio Perez
Force India,Nico Hulkenberg
Toro Rosso,Max Verstappen
Toro Rosso,Carlos Sainz
Sauber,Marcus Ericsson
Sauber,Felipe Nasr
McLaren,Fernando Alonso
McLaren,Jenson Button
Manor Racing,Rio Haryanto
Manor Racing,Pascal Wehrlein
Haas,Romain Grosjean
Haas,Esteban Gutierrez
Renault,Kevin Magnussen
Renault,Jolyon Palmer

races.txt:

Australian Grand Prix,58
Bahrain Grand Prix,57
Chinese Grand Prix,56
Russian Grand Prix,53
Spanish Grand Prix,66
Monaco Grand Prix,78
Canadian Grand Prix,70
European Grand Prix,51
Austrian Grand Prix,71
British Grand Prix,52
Hungarian Grand Prix,70
German Grand Prix,67
Belgian Grand Prix,44
Italian Grand Prix,53
Singapore Grand Prix,61
Malaysian Grand Prix,56
Japanese Grand Prix,53
United States Grand Prix,56
Mexican Grand Prix,71
Brazilian Grand Prix,71
Abu Dhabi Grand Prix,55

这是我的计划:

import java.util.*;
import java.io.*;

public class Race {

   // main method
   public static void main (String[] args) throws FileNotFoundException{
      // Race and Lap array
      String[] race = getRace(loadRace());
      int[] lap = getLap(loadRace());

      // Random race and get number of lap
      String raceName = race[chooseRace(race, lap)];
      int lapNum = lap[chooseRace(race, lap)];

      System.out.println("RACE: " + raceName  + ", LAP: "+ lapNum);
      System.out.println();

      // Drivers roster
      String[] driverName = getDrivers();
      String[] teamName = getTeam();
      System.out.println("#\tDriver Roster");
      System.out.println("-\t-------------");

      for(int i=1;i<=22;i++){
            System.out.println(i + "\t" + driverName[i-1] + " (" + teamName[i-1] + ")");
      }

      // Simulate the race
      int[] finalPos = simulateRace(qualifying(), lapNum);

      //Print final result
      System.out.println();
      System.out.println(raceName.toUpperCase() + " RESULTS");
      printResult(finalPos, driverName, teamName);
   }////////////////////////////////////////////////end main


      // method gets the drivers' names  
      public static String[] getDrivers() throws FileNotFoundException{
       // Count line
      Scanner input = new Scanner(new File("drivers.txt"));
      int countLine = 0;

      while (input.hasNextLine()) {
         input.nextLine();         
         countLine++;

      }

      // Load array
      String[] data = new String[countLine];
      countLine=0;   
      input = new Scanner(new File("drivers.txt"));

      while (input.hasNextLine()){             
         data[countLine] = input.nextLine();
         countLine++;
      }
      // Driver name only
      for(int i =0; i<=21; i++){
         data[i] = data[i].substring(data[i].indexOf(",")+1);
      }

      return data;   
   }

   // method gets the name of the teams.
   public static String[] getTeam() throws FileNotFoundException{
      // Count line
      Scanner input = new Scanner(new File("drivers.txt"));
      int countLine=0;

      while (input.hasNextLine()) {
         input.nextLine();         
         countLine++;

      }

      // Load array
      String[] data = new String[countLine];
      countLine=0;   
      input = new Scanner(new File("drivers.txt"));

      while (input.hasNextLine()){             
         data[countLine] = input.nextLine();
         countLine++;
      }
      // Team name only
      for(int i =0; i<=21; i++){
         data[i] = data[i].substring(0, data[i].indexOf(","));
      }

      return data; 

   }

   // method loads the race to array
   public static String[] loadRace() throws FileNotFoundException{
      //Count line
      Scanner input = new Scanner(new File("races.txt"));
      int countLine=0;

      while (input.hasNextLine()) {
         input.nextLine();         
         countLine++;

      }

      //Load array
      String[] data = new String[countLine];
      countLine=0;   
      input = new Scanner(new File("races.txt"));

      while (input.hasNextLine()){           
         data[countLine]=input.nextLine();
         countLine++;
      }

      return data;   
   }

   // method to get lap array
   public static int[] getLap(String[] array ){
      int[] lap = new int[array.length];

      for(int i=0; i<lap.length; i++){
         lap[i] = Integer.parseInt(array[i].substring(array[i].indexOf(",")+1));
      }

      return lap;
   }

   // method to get race array
   public static String[] getRace(String[] array) {
      String[] race = new String[array.length];

      for(int i =0; i<21; i++){
         race[i] = array[i].substring(0, array[i].indexOf(","));
      }

      return race;
   }

   //Random race
   public static int chooseRace(String[] raceName, int[] raceLap) {

      Random rand = new Random();
      int index = rand.nextInt(raceLap.length);

      return index;
   } 

   // method for qualifying positions at the beginning.
   public static int[] qualifying(){
      // Create position array
      int[] racePos = new int[22];
      for (int i=0; i<22; i++){
         racePos[i] = i+1;
      }

      Random rand = new Random();
      int temp = 0;
      int num = 0;
      // Random the position
      for(int i = racePos.length-1; i >= 0; i--) {
         num = rand.nextInt(racePos.length);
         temp = racePos[num];
         racePos[num] = racePos[i];
         racePos[i] = temp;
      }   
      System.out.println("\n Qualifying positions: " + Arrays.toString(racePos));

      return racePos; 
   }


   // method simulate 1 lap
   public static int[] raceOneLap(int[] racePos) {
      int temp;
      Random rand = new Random();

      // Switch position
      for(int i = 0; i< racePos.length-1; i++) {
         if(rand.nextInt(2) == 1) {
            temp = racePos[i];
            racePos[i]= racePos[i+1];
            racePos[i+1] = temp;
         }
      }

      return racePos;
   }

    // method simulates the race
    public static int[] simulateRace(int[] racePos, int lap){
      int[] currentPos = racePos;
      // Simulate the race with many lap
      for (int i=1; i <= lap; i++){
         currentPos = raceOneLap(racePos);
         System.out.println("After lap " + i + ":" + Arrays.toString(currentPos));
      }

      return currentPos;

    }

    // method prints the final result.
    public static void printResult(int[] position, String[] driverName, String[] teamName) {
       int[] finalPos = position;
       int index;

       System.out.println("\nPlace\tDriver");
       System.out.println("-----\t------");

       for(int i=1; i<22; i++) {
         index = 0;   
         while(finalPos[index] != i) {
            index++;
         }
         System.out.println(i + "\t"+ driverName[index] + " ("+ teamName[index]+")"); 
       }
    }

}

这是此示例输出:

# Driver Roster
 - -------------
 1 Lewis Hamilton (Mercedes)
 2 Nico Rosberg (Mercedes)
 3 Sebastian Vettel (Ferrari)
CS  141:    Java 1
4
 4 Kimi Raikkonnen (Ferrari)
 5 Felipe Massa (Williams)
 6 Valtteri Bottas (Williams)
 7 Daniel Ricciardo (Red Bull Racing)
 8 Daniil Kvyat (Red Bull Racing)
 9 Sergio Perez (Force India)
 10 Nico Hulkenberg (Force India)
 11 Max Verstappen (Toro Rosso)
 12 Carlos Sainz (Toro Rosso)
 13 Marcus Ericsson (Sauber)
 14 Felipe Nasr (Sauber)
 15 Fernando Alonso (McLaren)
 16 Jenson Button (McLaren)
 17 Rio Haryanto (Manor Racing)
 18 Pascal Wehrlein (Manor Racing)
 19 Romain Grosjean (Haas)
 20 Esteban Gutierrez (Haas)
 21 Kevin Magnussen (Renault)
 22 Jolyon Palmer (Renault)

 Qualifying positions: [11, 12, 7, 1, 10, 3, 13, 16, 6, 15, 19, 9, 21, 14, 22, 5, 20, 8, 18, 4, 17, 2]
 After lap 1: [12, 7, 11, 10, 1, 13, 3, 6, 16, 19, 15, 21, 14, 22, 9, 20, 8, 18, 5, 4, 17, 2]
 After lap 2: [7, 12, 11, 1, 10, 13, 3, 16, 6, 15, 19, 14, 21, 22, 9, 8, 20, 5, 4, 17, 2, 18]
 After lap 3: [7, 11, 12, 1, 10, 3, 16, 13, 6, 19, 14, 15, 21, 22, 9, 20, 8, 4, 5, 2, 17, 18]
 After lap 4: [11, 7, 12, 1, 3, 10, 16, 6, 19, 14, 15, 21, 13, 22, 20, 8, 4, 5, 2, 17, 18, 9]
 After lap 5: [7, 12, 1, 3, 10, 16, 6, 11, 19, 14, 21, 15, 22, 13, 8, 4, 5, 2, 20, 18, 17, 9]
 After lap 6: [7, 12, 1, 3, 16, 6, 10, 19, 14, 11, 21, 22, 13, 15, 8, 5, 4, 20, 2, 18, 9, 17]
 After lap 7: [7, 1, 12, 3, 6, 16, 10, 19, 11, 14, 22, 13, 21, 8, 15, 4, 5, 2, 18, 20, 17, 9]
 After lap 8: [7, 12, 3, 1, 6, 16, 19, 10, 14, 22, 13, 21, 11, 15, 4, 8, 2, 18, 5, 17, 9, 20]
 After lap 9: [12, 3, 7, 6, 16, 19, 1, 10, 22, 13, 14, 21, 15, 4, 8, 11, 18, 2, 5, 17, 9, 20]
 After lap 10: [3, 12, 7, 16, 19, 1, 10, 22, 6, 13, 14, 15, 4, 21, 8, 18, 2, 5, 17, 11, 9, 20]
 After lap 11: [12, 7, 16, 3, 19, 1, 10, 22, 6, 14, 13, 15, 21, 8, 4, 2, 5, 17, 18, 11, 9, 20]
 After lap 12: [7, 16, 3, 19, 1, 10, 12, 22, 14, 13, 15, 6, 21, 4, 8, 2, 5, 17, 11, 18, 20, 9]
 After lap 13: [7, 16, 3, 19, 1, 12, 22, 14, 13, 10, 15, 21, 4, 6, 2, 5, 8, 11, 17, 20, 18, 9]
 After lap 14: [7, 3, 16, 19, 12, 1, 14, 22, 13, 15, 10, 4, 6, 21, 5, 8, 11, 2, 20, 17, 18, 9]
 After lap 15: [7, 3, 19, 12, 16, 14, 1, 13, 22, 10, 15, 6, 21, 4, 5, 11, 8, 2, 17, 18, 9, 20]
 After lap 16: [3, 19, 12, 7, 14, 16, 13, 22, 1, 10, 15, 21, 6, 4, 11, 5, 2, 8, 18, 17, 20, 9]
 After lap 17: [3, 12, 7, 14, 16, 13, 19, 22, 1, 10, 15, 6, 4, 21, 5, 2, 11, 8, 18, 20, 17, 9]
 After lap 18: [12, 7, 3, 16, 14, 13, 19, 22, 10, 1, 6, 4, 15, 21, 2, 11, 5, 8, 20, 18, 17, 9]
 After lap 19: [7, 12, 3, 14, 16, 19, 13, 22, 1, 10, 6, 4, 21, 15, 11, 2, 8, 5, 18, 17, 20, 9]
 After lap 20: [12, 3, 7, 14, 19, 16, 22, 1, 13, 6, 4, 21, 15, 11, 10, 8, 2, 18, 17, 5, 20, 9]
 After lap 21: [3, 12, 7, 14, 19, 16, 1, 13, 6, 4, 21, 22, 15, 10, 8, 11, 2, 17, 18, 5, 20, 9]
 After lap 22: [3, 7, 12, 14, 19, 1, 16, 6, 13, 4, 21, 22, 15, 8, 10, 11, 17, 18, 2, 5, 20, 9]
 After lap 23: [7, 12, 14, 3, 19, 1, 16, 6, 13, 4, 22, 21, 15, 10, 11, 8, 18, 2, 17, 20, 5, 9]
 After lap 24: [7, 12, 14, 3, 19, 1, 16, 6, 13, 22, 4, 15, 10, 11, 21, 8, 18, 17, 20, 2, 5, 9]
 After lap 25: [7, 12, 14, 3, 1, 16, 19, 13, 6, 22, 4, 10, 11, 21, 8, 18, 15, 20, 2, 5, 17, 9]
 After lap 26: [7, 14, 12, 3, 16, 19, 1, 13, 6, 22, 10, 11, 21, 8, 18, 15, 4, 2, 5, 17, 20, 9]
 After lap 27: [14, 12, 7, 3, 16, 19, 13, 6, 22, 10, 1, 21, 8, 18, 15, 11, 2, 4, 17, 5, 9, 20]
 After lap 28: [14, 12, 7, 3, 19, 13, 6, 22, 10, 16, 1, 21, 18, 8, 15, 11, 2, 4, 17, 9, 5, 20]
 After lap 29: [14, 7, 12, 3, 19, 13, 22, 6, 16, 10, 1, 21, 8, 18, 15, 11, 4, 2, 9, 17, 5, 20]
 After lap 30: [7, 14, 12, 3, 19, 13, 6, 22, 16, 10, 1, 8, 21, 15, 18, 4, 2, 11, 17, 9, 5, 20]
 After lap 31: [14, 12, 3, 7, 19, 13, 22, 16, 10, 1, 8, 21, 15, 6, 4, 2, 18, 11, 9, 17, 20, 5]
 After lap 32: [12, 14, 3, 19, 13, 22, 16, 7, 1, 8, 21, 10, 6, 4, 15, 18, 2, 11, 9, 17, 20, 5]
 After lap 33: [14, 3, 19, 13, 22, 12, 7, 16, 1, 8, 10, 6, 21, 15, 4, 18, 2, 11, 17, 20, 5, 9]
 After lap 34: [3, 19, 14, 13, 12, 22, 16, 7, 1, 8, 10, 21, 15, 6, 18, 4, 11, 17, 20, 5, 2, 9]
 After lap 35: [19, 14, 3, 13, 22, 16, 12, 7, 1, 8, 10, 21, 6, 15, 4, 11, 17, 20, 5, 18, 2, 9]
 After lap 36: [19, 14, 13, 22, 16, 3, 7, 1, 8, 12, 10, 21, 15, 6, 4, 11, 20, 17, 5, 18, 9, 2]
 After lap 37: [19, 14, 13, 16, 3, 7, 1, 22, 8, 12, 21, 10, 15, 6, 4, 20, 11, 5, 18, 17, 9, 2]
 After lap 38: [19, 13, 14, 16, 3, 1, 7, 8, 12, 21, 10, 22, 6, 4, 20, 11, 5, 15, 17, 9, 2, 18]
 After lap 39: [13, 19, 16, 14, 3, 1, 8, 12, 7, 21, 10, 22, 6, 20, 4, 11, 5, 17, 15, 9, 2, 18]
 After lap 40: [13, 19, 14, 16, 3, 8, 12, 1, 21, 7, 10, 22, 20, 4, 6, 5, 17, 11, 15, 9, 18, 2]
 After lap 41: [19, 13, 16, 3, 14, 12, 8, 1, 7, 10, 21, 22, 20, 6, 4, 5, 17, 15, 11, 18, 2, 9]
 After lap 42: [19, 16, 13, 14, 3, 8, 1, 12, 7, 10, 21, 22, 6, 4, 5, 17, 20, 15, 11, 18, 9, 2]
 After lap 43: [19, 13, 16, 14, 8, 1, 3, 7, 12, 21, 10, 22, 6, 5, 17, 20, 15, 4, 18, 9, 2, 11]
 After lap 44: [13, 19, 16, 8, 14, 3, 7, 1, 21, 12, 22, 10, 5, 17, 20, 15, 6, 4, 9, 18, 2, 11]
 After lap 45: [13, 19, 16, 8, 3, 14, 7, 21, 1, 22, 12, 10, 5, 20, 15, 17, 6, 4, 9, 2, 18, 11]
 After lap 46: [13, 19, 8, 16, 3, 7, 14, 1, 22, 12, 21, 10, 5, 15, 17, 20, 4, 6, 9, 2, 11, 18]
 After lap 47: [13, 8, 16, 19, 3, 14, 1, 22, 7, 12, 21, 5, 10, 17, 15, 4, 20, 6, 2, 9, 11, 18]
 After lap 48: [13, 8, 16, 19, 3, 1, 14, 22, 12, 7, 5, 10, 17, 15, 21, 4, 6, 20, 2, 11, 9, 18]
 After lap 49: [13, 16, 19, 8, 3, 1, 22, 14, 12, 5, 10, 17, 7, 15, 21, 6, 20, 2, 4, 11, 9, 18]
 After lap 50: [16, 13, 19, 8, 1, 3, 14, 22, 5, 10, 12, 17, 15, 7, 21, 6, 2, 4, 11, 20, 9, 18]
 After lap 51: [16, 19, 8, 1, 13, 3, 22, 5, 14, 12, 17, 15, 10, 21, 6, 2, 4, 11, 20, 7, 9, 18]
 After lap 52: [19, 8, 16, 13, 3, 22, 5, 14, 12, 1, 17, 10, 15, 21, 2, 6, 11, 20, 7, 9, 18, 4]
CS  141:    Java 1
5
 After lap 53: [19, 8, 13, 16, 22, 3, 5, 14, 12, 17, 10, 1, 15, 2, 6, 21, 11, 20, 9, 18, 4, 7]
 After lap 54: [19, 8, 16, 13, 22, 5, 14, 12, 17, 10, 1, 15, 3, 2, 21, 11, 6, 9, 20, 18, 7, 4]
 After lap 55: [19, 8, 16, 13, 5, 14, 22, 17, 10, 1, 15, 12, 3, 21, 11, 2, 6, 20, 9, 7, 18, 4]
 After lap 56: [19, 16, 13, 8, 14, 22, 17, 5, 1, 10, 12, 15, 21, 11, 2, 6, 20, 9, 7, 3, 18, 4]
 After lap 57: [16, 13, 19, 8, 22, 17, 14, 5, 1, 12, 15, 21, 11, 10, 2, 6, 9, 7, 3, 18, 4, 20]
 After lap 58: [13, 16, 8, 19, 17, 14, 22, 1, 12, 5, 21, 11, 15, 2, 6, 9, 7, 10, 3, 18, 20, 4]
 After lap 59: [13, 8, 16, 19, 14, 22, 1, 12, 17, 5, 21, 15, 11, 6, 2, 7, 10, 9, 3, 18, 4, 20]
 After lap 60: [13, 8, 16, 14, 22, 19, 1, 12, 17, 5, 15, 11, 21, 2, 6, 7, 10, 9, 3, 18, 4, 20]
 After lap 61: [13, 8, 14, 16, 22, 1, 19, 12, 17, 15, 11, 21, 5, 6, 2, 10, 9, 7, 3, 18, 4, 20]
 After lap 62: [8, 14, 16, 22, 13, 1, 12, 17, 15, 19, 11, 5, 6, 2, 10, 9, 21, 7, 18, 3, 20, 4]
 After lap 63: [14, 16, 22, 8, 1, 13, 17, 15, 19, 12, 11, 5, 2, 6, 9, 21, 7, 10, 18, 3, 4, 20]
 After lap 64: [14, 16, 22, 8, 13, 17, 15, 19, 12, 1, 5, 2, 6, 11, 9, 7, 10, 18, 3, 4, 21, 20]
 After lap 65: [16, 22, 8, 13, 14, 15, 19, 17, 1, 12, 5, 6, 11, 9, 2, 7, 18, 3, 4, 10, 21, 20]
 After lap 66: [16, 22, 8, 13, 15, 14, 17, 19, 12, 1, 6, 11, 9, 5, 2, 18, 7, 3, 10, 21, 20, 4]
 After lap 67: [16, 22, 8, 13, 14, 15, 19, 12, 1, 17, 11, 6, 9, 5, 18, 7, 3, 2, 21, 10, 4, 20]
 After lap 68: [22, 8, 13, 14, 15, 16, 19, 1, 12, 17, 11, 6, 9, 18, 7, 5, 2, 21, 3, 4, 10, 20]
 After lap 69: [8, 13, 14, 15, 16, 19, 1, 22, 17, 12, 11, 9, 6, 7, 18, 2, 21, 3, 5, 10, 20, 4]
 After lap 70: [13, 8, 14, 15, 19, 16, 22, 17, 1, 12, 11, 9, 7, 18, 6, 2, 3, 21, 10, 20, 5, 4]
 After lap 71: [8, 14, 13, 19, 16, 15, 17, 1, 12, 22, 9, 11, 7, 18, 6, 3, 21, 10, 20, 5, 2, 4]

 AUSTRIAN GRAND PRIX RESULTS

 Place Driver
 ----- ------
 1: Daniil Kvyat (Red Bull Racing)
 2: Felipe Nasr (Sauber)
 3: Marcus Ericsson (Sauber)
 4: Romain Grosjean (Haas)
 5: Jenson Button (McLaren)
 6: Fernando Alonso (McLaren)
 7: Rio Haryanto (Manor Racing)
 8: Lewis Hamilton (Mercedes)
 9: Carlos Sainz (Toro Rosso)
 10: Jolyon Palmer (Renault)
 11: Sergio Perez (Force India)
 12: Max Verstappen (Toro Rosso)
 13: Daniel Ricciardo (Red Bull Racing)
 14: Pascal Wehrlein (Manor Racing)
 15: Valtteri Bottas (Williams)
 16: Sebastian Vettel (Ferrari)
 17: Kevin Magnussen (Renault)
 18: Nico Hulkenberg (Force India)
 19: Esteban Gutierrez (Haas)
 20: Felipe Massa (Williams)
 21: Nico Rosberg (Mercedes)
 22: Kimi Raikkonnen (Ferrari)

现在我想输出驱动程序的名称而不是数字。例如:

Qualifying positions: Button, Verstappen, Vettel, Hamilton, Palmer, Wehrlein, Ericsson, Ricciardo,
Rosberg, Hulkenberg, Perez, Alonso, Raikkonnen, Sainz, Grosjean, Magnussen, Massa, Nasr,
Gutierrez, Kvyat, Bottas, Haryanto
 After lap 1: Verstappen, Vettel, Button, Palmer, Wehrlein, Hamilton, Ricciardo, Rosberg, Ericsson,
Hulkenberg, Perez, Raikkonnen, Sainz, Grosjean, Alonso, Massa, Nasr, Magnussen, Kvyat, Bottas,
Gutierrez, Haryanto
CS  141:    Java 1
6
 After lap 2: Verstappen, Vettel, Button, Palmer, Hamilton, Wehrlein, Rosberg, Ericsson, Hulkenberg,
Perez, Ricciardo, Raikkonnen, Sainz, Alonso, Grosjean, Massa, Nasr, Magnussen, Bottas, Gutierrez,
Haryanto, Kvyat
 After lap 3: Verstappen, Vettel, Palmer, Button, Hamilton, Rosberg, Ericsson, Hulkenberg, Perez,
Ricciardo, Raikkonnen, Wehrlein, Sainz, Grosjean, Alonso, Nasr, Massa, Magnussen, Bottas,
Haryanto, Gutierrez, Kvyat
 After lap 4: Verstappen, Palmer, Button, Vettel, Hamilton, Ericsson, Rosberg, Perez, Hulkenberg,
Ricciardo, Raikkonnen, Sainz, Wehrlein, Grosjean, Nasr, Massa, Alonso, Bottas, Magnussen,
Gutierrez, Haryanto, Kvyat
 After lap 5: Palmer, Button, Verstappen, Vettel, Hamilton, Rosberg, Ericsson, Perez, Ricciardo,
Hulkenberg, Sainz, Raikkonnen, Grosjean, Wehrlein, Nasr, Massa, Bottas, Magnussen, Alonso,
Gutierrez, Kvyat, Haryanto
 After lap 6: Button, Verstappen, Vettel, Palmer, Rosberg, Hamilton, Perez, Ericsson, Hulkenberg,
Sainz, Raikkonnen, Ricciardo, Grosjean, Nasr, Massa, Wehrlein, Bottas, Alonso, Magnussen,
Gutierrez, Kvyat, Haryanto
 After lap 7: Verstappen, Vettel, Palmer, Rosberg, Button, Hamilton, Perez, Hulkenberg, Sainz,
Ericsson, Raikkonnen, Ricciardo, Grosjean, Massa, Nasr, Bottas, Alonso, Wehrlein, Gutierrez,
Kvyat, Haryanto, Magnussen
 After lap 8: Verstappen, Vettel, Palmer, Button, Rosberg, Perez, Hulkenberg, Hamilton, Sainz,
Ericsson, Raikkonnen, Ricciardo, Massa, Nasr, Grosjean, Bottas, Wehrlein, Alonso, Kvyat,
Gutierrez, Haryanto, Magnussen
 After lap 9: Verstappen, Palmer, Vettel, Button, Perez, Hulkenberg, Rosberg, Sainz, Hamilton,
Raikkonnen, Ericsson, Ricciardo, Nasr, Massa, Grosjean, Wehrlein, Bottas, Alonso, Kvyat, Haryanto,
Magnussen, Gutierrez
 After lap 10: Verstappen, Palmer, Vettel, Perez, Hulkenberg, Rosberg, Button, Hamilton, Sainz,
Raikkonnen, Ricciardo, Nasr, Massa, Ericsson, Grosjean, Wehrlein, Alonso, Kvyat, Bottas, Haryanto,
Magnussen, Gutierrez
 After lap 11: Verstappen, Palmer, Perez, Hulkenberg, Rosberg, Button, Vettel, Hamilton, Sainz,
Ricciardo, Raikkonnen, Massa, Nasr, Grosjean, Wehrlein, Alonso, Ericsson, Kvyat, Haryanto,
Magnussen, Bottas, Gutierrez
 After lap 12: Verstappen, Palmer, Perez, Rosberg, Hulkenberg, Button, Vettel, Sainz, Hamilton,
Ricciardo, Massa, Nasr, Raikkonnen, Wehrlein, Alonso, Ericsson, Kvyat, Haryanto, Magnussen,
Bottas, Grosjean, Gutierrez
 After lap 13: Verstappen, Palmer, Rosberg, Hulkenberg, Button, Vettel, Sainz, Hamilton, Ricciardo,
Perez, Massa, Raikkonnen, Nasr, Wehrlein, Alonso, Ericsson, Haryanto, Magnussen, Bottas, Kvyat,
Gutierrez, Grosjean
 After lap 14: Verstappen, Palmer, Rosberg, Button, Hulkenberg, Sainz, Vettel, Ricciardo, Hamilton,
Perez, Massa, Nasr, Raikkonnen, Wehrlein, Ericsson, Haryanto, Alonso, Bottas, Kvyat, Magnussen,
Gutierrez, Grosjean
 After lap 15: Palmer, Verstappen, Button, Hulkenberg, Rosberg, Vettel, Sainz, Hamilton, Perez, Massa,
Nasr, Ricciardo, Wehrlein, Ericsson, Haryanto, Alonso, Raikkonnen, Bottas, Magnussen, Gutierrez,
Kvyat, Grosjean
 After lap 16: Palmer, Button, Verstappen, Rosberg, Vettel, Sainz, Hamilton, Hulkenberg, Massa, Perez,
Nasr, Wehrlein, Ericsson, Ricciardo, Haryanto, Raikkonnen, Bottas, Alonso, Magnussen, Kvyat,
Grosjean, Gutierrez
 After lap 17: Button, Palmer, Rosberg, Verstappen, Sainz, Vettel, Hamilton, Massa, Hulkenberg, Nasr,
Perez, Wehrlein, Ericsson, Haryanto, Ricciardo, Raikkonnen, Bottas, Magnussen, Alonso, Kvyat,
Gutierrez, Grosjean
 After lap 18: Button, Palmer, Verstappen, Rosberg, Sainz, Hamilton, Massa, Vettel, Hulkenberg, Nasr,
Wehrlein, Perez, Haryanto, Ericsson, Raikkonnen, Bottas, Magnussen, Alonso, Kvyat, Gutierrez,
Ricciardo, Grosjean
 After lap 19: Palmer, Verstappen, Button, Rosberg, Hamilton, Sainz, Massa, Hulkenberg, Vettel,
Wehrlein, Nasr, Haryanto, Ericsson, Raikkonnen, Perez, Magnussen, Alonso, Kvyat, Bottas,
Ricciardo, Grosjean, Gutierrez
 After lap 20: Palmer, Button, Verstappen, Hamilton, Rosberg, Sainz, Massa, Hulkenberg, Vettel, Nasr,
Wehrlein, Ericsson, Raikkonnen, Perez, Haryanto, Magnussen, Alonso, Kvyat, Ricciardo, Bottas,
Grosjean, Gutierrez
 After lap 21: Button, Palmer, Hamilton, Rosberg, Verstappen, Massa, Hulkenberg, Vettel, Nasr,
Wehrlein, Ericsson, Sainz, Raikkonnen, Haryanto, Magnussen, Alonso, Kvyat, Ricciardo, Perez,
Bottas, Grosjean, Gutierrez
 After lap 22: Palmer, Button, Rosberg, Verstappen, Hamilton, Massa, Vettel, Nasr, Hulkenberg,
Ericsson, Wehrlein, Sainz, Raikkonnen, Haryanto, Magnussen, Kvyat, Ricciardo, Perez, Alonso,
Bottas, Gutierrez, Grosjean
 After lap 23: Palmer, Rosberg, Button, Verstappen, Massa, Vettel, Nasr, Hamilton, Ericsson, Wehrlein,
Hulkenberg, Raikkonnen, Sainz, Magnussen, Kvyat, Ricciardo, Haryanto, Alonso, Perez, Bottas,
Grosjean, Gutierrez
 After lap 24: Rosberg, Button, Verstappen, Palmer, Massa, Vettel, Hamilton, Nasr, Ericsson,
Hulkenberg, Wehrlein, Sainz, Magnussen, Raikkonnen, Kvyat, Ricciardo, Haryanto, Perez, Alonso,
Grosjean, Bottas, Gutierrez
 After lap 25: Button, Verstappen, Palmer, Massa, Rosberg, Hamilton, Vettel, Ericsson, Nasr,
Hulkenberg, Wehrlein, Magnussen, Raikkonnen, Kvyat, Sainz, Ricciardo, Haryanto, Alonso, Perez,
Bottas, Grosjean, Gutierrez
CS  141:    Java 1
7
 After lap 26: Button, Verstappen, Massa, Rosberg, Hamilton, Palmer, Ericsson, Vettel, Hulkenberg,
Wehrlein, Magnussen, Raikkonnen, Kvyat, Sainz, Ricciardo, Nasr, Haryanto, Perez, Bottas, Alonso,
Grosjean, Gutierrez
 After lap 27: Verstappen, Button, Rosberg, Massa, Hamilton, Ericsson, Palmer, Vettel, Wehrlein,
Hulkenberg, Raikkonnen, Magnussen, Kvyat, Ricciardo, Sainz, Nasr, Perez, Bottas, Haryanto, Alonso,
Gutierrez, Grosjean
 After lap 28: Button, Rosberg, Massa, Hamilton, Ericsson, Palmer, Verstappen, Vettel, Hulkenberg,
Wehrlein, Magnussen, Kvyat, Raikkonnen, Ricciardo, Sainz, Nasr, Bottas, Haryanto, Alonso, Perez,
Grosjean, Gutierrez
 After lap 29: Button, Massa, Rosberg, Ericsson, Hamilton, Palmer, Verstappen, Vettel, Hulkenberg,
Wehrlein, Kvyat, Raikkonnen, Magnussen, Ricciardo, Nasr, Bottas, Haryanto, Alonso, Perez, Sainz,
Gutierrez, Grosjean
 After lap 30: Massa, Rosberg, Ericsson, Hamilton, Palmer, Verstappen, Button, Vettel, Wehrlein,
Kvyat, Hulkenberg, Magnussen, Raikkonnen, Ricciardo, Bottas, Nasr, Alonso, Haryanto, Sainz, Perez,
Grosjean, Gutierrez
 After lap 31: Massa, Ericsson, Rosberg, Palmer, Hamilton, Button, Verstappen, Wehrlein, Kvyat,
Hulkenberg, Vettel, Raikkonnen, Ricciardo, Magnussen, Bottas, Alonso, Haryanto, Nasr, Perez,
Sainz, Grosjean, Gutierrez
 After lap 32: Massa, Ericsson, Palmer, Hamilton, Button, Rosberg, Verstappen, Wehrlein, Hulkenberg,
Vettel, Kvyat, Ricciardo, Raikkonnen, Bottas, Alonso, Haryanto, Magnussen, Nasr, Perez, Sainz,
Gutierrez, Grosjean
 After lap 33: Massa, Palmer, Hamilton, Button, Ericsson, Rosberg, Wehrlein, Verstappen, Hulkenberg,
Vettel, Ricciardo, Raikkonnen, Kvyat, Alonso, Bottas, Magnussen, Haryanto, Nasr, Sainz, Perez,
Grosjean, Gutierrez
 After lap 34: Palmer, Hamilton, Button, Massa, Ericsson, Wehrlein, Rosberg, Hulkenberg, Vettel,
Verstappen, Raikkonnen, Kvyat, Alonso, Bottas, Ricciardo, Magnussen, Haryanto, Sainz, Perez,
Grosjean, Nasr, Gutierrez
 After lap 35: Hamilton, Button, Palmer, Ericsson, Wehrlein, Rosberg, Hulkenberg, Vettel, Verstappen,
Massa, Kvyat, Alonso, Raikkonnen, Bottas, Magnussen, Ricciardo, Haryanto, Perez, Sainz, Grosjean,
Gutierrez, Nasr
 After lap 36: Hamilton, Button, Ericsson, Wehrlein, Palmer, Rosberg, Hulkenberg, Vettel, Massa,
Kvyat, Alonso, Verstappen, Raikkonnen, Magnussen, Bottas, Haryanto, Ricciardo, Sainz, Perez,
Gutierrez, Grosjean, Nasr
 After lap 37: Hamilton, Button, Wehrlein, Palmer, Ericsson, Rosberg, Vettel, Massa, Kvyat, Alonso,
Hulkenberg, Raikkonnen, Verstappen, Magnussen, Bottas, Ricciardo, Haryanto, Sainz, Gutierrez,
Perez, Grosjean, Nasr
 After lap 38: Hamilton, Wehrlein, Palmer, Button, Ericsson, Vettel, Rosberg, Massa, Alonso,
Hulkenberg, Kvyat, Raikkonnen, Magnussen, Verstappen, Bottas, Haryanto, Ricciardo, Gutierrez,
Sainz, Perez, Nasr, Grosjean
 After lap 39: Wehrlein, Palmer, Hamilton, Button, Vettel, Rosberg, Ericsson, Massa, Alonso,
Hulkenberg, Kvyat, Magnussen, Raikkonnen, Verstappen, Haryanto, Ricciardo, Bottas, Gutierrez,
Sainz, Nasr, Perez, Grosjean
 After lap 40: Palmer, Hamilton, Button, Vettel, Rosberg, Ericsson, Wehrlein, Massa, Alonso, Kvyat,
Magnussen, Raikkonnen, Verstappen, Haryanto, Ricciardo, Bottas, Gutierrez, Hulkenberg, Nasr,
Perez, Sainz, Grosjean
 After lap 41: Hamilton, Button, Vettel, Palmer, Ericsson, Rosberg, Massa, Wehrlein, Kvyat, Alonso,
Raikkonnen, Verstappen, Haryanto, Magnussen, Bottas, Gutierrez, Hulkenberg, Ricciardo, Perez,
Sainz, Grosjean, Nasr
 After lap 42: Hamilton, Vettel, Button, Palmer, Ericsson, Rosberg, Wehrlein, Kvyat, Massa,
Raikkonnen, Verstappen, Haryanto, Magnussen, Alonso, Bottas, Hulkenberg, Gutierrez, Ricciardo,
Perez, Sainz, Nasr, Grosjean
 After lap 43: Vettel, Button, Hamilton, Palmer, Ericsson, Rosberg, Wehrlein, Kvyat, Raikkonnen,
Verstappen, Haryanto, Magnussen, Massa, Bottas, Alonso, Hulkenberg, Gutierrez, Perez, Ricciardo,
Nasr, Sainz, Grosjean
 After lap 44: Button, Hamilton, Palmer, Ericsson, Rosberg, Vettel, Wehrlein, Raikkonnen, Kvyat,
Haryanto, Magnussen, Verstappen, Bottas, Massa, Alonso, Gutierrez, Perez, Hulkenberg, Ricciardo,
Sainz, Nasr, Grosjean
....

有人可以帮我吗?我不知道如何解决它。  谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于:

System.out.println("\n Qualifying positions: " + Arrays.toString(racePos));

您应该将其更改为循环以通过racePos数组并从drivers数组中获取驱动程序名称(driverName) - 确保数组不在范围之外(目前正在使用) - 使用值racePos数组的每个元素都作为索引。