2014-02-07 42 views
1

在此程序中,我試圖隨機生成名稱,機場目的地/到達地點以及他們隨時間飛到那裏的那一天。當文件中的行數超過10000個條目並且少於100,000個條目時,我遇到了終止程序的問題。我想出的一個解決方案是創建一個名爲minimumInputs的數組。這相當於可用機場的數量。在flightManifest方法中,我確保添加與給定飛機生成的座位數相等的條目數。一旦每個機場文件達到10,000個,但不超過100,000個條目,我就無法弄清楚如何讓它停止。謝謝大家的幫助!限制文件中輸入的數量Java

編輯:固定!這是編輯while循環來解決問題。

while (minimumInputs[0] < numberOfEntries && minimumInputs[1] < numberOfEntries && minimumInputs[2] < numberOfEntries && minimumInputs[3] < numberOfEntries && minimumInputs[4] < numberOfEntries && minimumInputs[5] < numberOfEntries && minimumInputs[6] < numberOfEntries && minimumInputs[7] < numberOfEntries && minimumInputs[8] < numberOfEntries && minimumInputs[9] < numberOfEntries && minimumInputs[10] < numberOfEntries && minimumInputs[11] < numberOfEntries && minimumInputs[12] < numberOfEntries && minimumInputs[13] < numberOfEntries && minimumInputs[14] < numberOfEntries && minimumInputs[15] < numberOfEntries && minimumInputs[16] < numberOfEntries && minimumInputs[17] < numberOfEntries && minimumInputs[18] < numberOfEntries && minimumInputs[19] < numberOfEntries && minimumInputs[20] < numberOfEntries && minimumInputs[21] < numberOfEntries && minimumInputs[22] < numberOfEntries && minimumInputs[23] < numberOfEntries && minimumInputs[24] < numberOfEntries) { 
      flightManifest(); 
     } 

import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 

public class OLE1 { 

    static String[] airports = {"LAX", "MSP", "FAR", "ATL", "ORD", "DFW", "DEN", "JFK", "SFO", "CLT", "LAS", "PHX", "IAH", "MIA", "PEK", "CAN", "HND", "HKG", "SIN", "LHR", "CDG", "LGW", "MUC", "FRA", "EDI"}; 
    static final int numberOfEntries = 10000; 
    static String[] firstNames = {"Isis", "Donnette", "Reyes", "Willis", "Kathy", "Elizbeth", "Long", "Jim", "Devorah", "Magda", "Maryetta", "Keturah", "Corrinne", "Shena", "Xiao", "Otha", "Hallie", "Pennie", "Dong", "Kristopher", "Eveline", "Ardella", "Tien", "Tianna", "Loren", "Many", "Anjelica", "Cecile", "Mae", "Jenae", "Sonya", "Dotty", "Florance", "Mittie", "Katia", "Nena", "Lu", "Janee", "Armando", "Leandro", "Claris", "Claudine", "Moriah", "Eddie", "Susan", "Rhoda", "Monnie", "Emelia", "Cory", "Ying"}; 
    static String[] lastNames = {"Belva", "Nam", "Liz", "Jeanett", "Corine", "Abe", "Olga", "Olevia", "Ernestine", "Joanne", "Sharyn", "Heidi", "Zachariah", "Sylvester", "Luetta", "Stephaine", "Garrett", "Debby", "Judi", "Noe", "Maybelle", "Eldora", "Roseann", "Madge", "Glayds", "Eleonore", "Josephine", "Quincy", "Alyson", "Earlene", "Clementina", "Jeri", "Kristel", "Carrol", "Zona", "Eileen", "Margherita", "Joline", "Terence", "Christinia", "Eldon", "Arleen", "Aimee", "Chanda", "Carin", "Prudence", "Tanja", "Kathlene", "Kareen", "Geneva"}; 
    static String[] middleInitial = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; 
    static int minimumInputs[] = new int[25]; 
    static int count = 0; 

    public static void main(String args[]) throws IOException { 

     System.out.println("Welcome to the airplane program. This program will go ahead and create a list of all the passengers that have gone through 25 different airports."); 
     System.out.println("The list of the airports we keep track of are listed below\n"); 
     int count = 0; 

     //for listing out the airport names 
     for (int i = 0; i < 5; i++) { 
      System.out.print(airports[count]); 
      count++; 
      System.out.print(" | " + airports[count]); 
      count++; 
      System.out.print(" | " + airports[count]); 
      count++; 
      System.out.print(" | " + airports[count]); 
      count++; 
      System.out.println(" | " + airports[count]); 
      count++; 
     } 
    /* 
     for (int i = 0; i < 1000; i++) { 
      if (flightSeatingAmount() >= 850) { 
       System.out.println("check this" + flightSeatingAmount()); 
      } 
      System.out.println(flightSeatingAmount()); 

     } 
    */ 

     //Need to add it so its not more than 100,000 entries in a file. 
     while(minimumInputs[0] < numberOfEntries || minimumInputs[1] < numberOfEntries || minimumInputs[2] < numberOfEntries || minimumInputs[3] < numberOfEntries || minimumInputs[4] < numberOfEntries || minimumInputs[5] < numberOfEntries || minimumInputs[6] < numberOfEntries || minimumInputs[7] < numberOfEntries || minimumInputs[8] < numberOfEntries || minimumInputs[9] < numberOfEntries || minimumInputs[10] < numberOfEntries || minimumInputs[11] < numberOfEntries || minimumInputs[12] < numberOfEntries || minimumInputs[13] < numberOfEntries || minimumInputs[14] < numberOfEntries || minimumInputs[15] < numberOfEntries || minimumInputs[16] < numberOfEntries || minimumInputs[17] < numberOfEntries || minimumInputs[18] < numberOfEntries || minimumInputs[19] < numberOfEntries || minimumInputs[20] < numberOfEntries || minimumInputs[21] < numberOfEntries || minimumInputs[22] < numberOfEntries || minimumInputs[23] < numberOfEntries || minimumInputs[24] < numberOfEntries) { 
      flightManifest(); 
     } 

     System.out.println("The files have been compiled."); 

    } 

    public static int flightSeatingAmount() { 
     int flight = (int)(50 + (Math.random() * 800)); 
     return flight; 
    } 

    public static String nameGenerator() { 
     String names = ""; 
     int firstAndLast = (int)(Math.random() * 50); 
     int initial = (int)(Math.random() * 26); 
     names = lastNames[firstAndLast] + "," + firstNames[firstAndLast] + "," + middleInitial[initial]; 
     return names; 
    } 

    public static int airportSelectionOne() { 
     int airportOne = (int)(Math.random() * (25 - 1)); 
     return airportOne; 
    } 

    public static int airportSelectionTwo() { 
     int airportTwo = (int)(Math.random() * (25 - 1)); 
     return airportTwo; 
    } 

    public static int flightTimeIntervalMinutes() { 
     int timing[] = {15, 30, 45, 00}; 
     int randomTiming = (int)(Math.random() * 4); 
     return timing[randomTiming]; 
    } 

    public static int flightTimeIntervalHours() { 
     int timing = (int)(Math.random() * 24); 
     return timing; 
    } 

    public static int flightDuration() { 
     int duration = 0; 

     if (airportSelectionOne() >= 0 && airportSelectionOne() <= 13 && airportSelectionTwo() >= 14 && airportSelectionTwo() <= 18) { 
      duration = 22; 
     } 
     if (airportSelectionOne() >= 0 && airportSelectionOne() <= 13 && airportSelectionTwo() >= 19 && airportSelectionTwo() <= 24) { 
      duration = 9; 
     } 
     if (airportSelectionOne() >= 14 && airportSelectionOne() <= 18 && airportSelectionTwo() >= 0 && airportSelectionTwo() <= 13) { 
      duration = 22; 
     } 
     if (airportSelectionOne() >= 14 && airportSelectionOne() <= 18 && airportSelectionTwo() >= 19 && airportSelectionTwo() <= 24) { 
      duration = 10; 
     } 
     if (airportSelectionOne() >= 19 && airportSelectionOne() <= 24 && airportSelectionTwo() >= 0 && airportSelectionTwo() <= 13) { 
      duration = 9; 
     } 
     if (airportSelectionOne() >= 19 && airportSelectionOne() <= 24 && airportSelectionTwo() >= 14 && airportSelectionTwo() <= 18) { 
      duration = 10; 
     } 
     if (airportSelectionOne() >= 0 && airportSelectionOne() <= 10 && airportSelectionTwo() >= 0 && airportSelectionTwo() <= 13) { 
      duration = 3; 
     } 
     if (airportSelectionOne() >= 14 && airportSelectionOne() <= 18 && airportSelectionTwo() >= 14 && airportSelectionTwo() <= 18) { 
      duration = 2; 
     } 
     if (airportSelectionOne() >= 19 && airportSelectionOne() <= 24 && airportSelectionTwo() >= 19 && airportSelectionTwo() <= 24) { 
      duration = 2; 
     } 

     return duration; 
    } 


    public static void flightManifest() throws IOException { 

     String departure = airports[airportSelectionOne()]; 
     String arrival = airports[airportSelectionTwo()]; 

     PrintWriter outputFileDeparture = new PrintWriter(new FileWriter(departure.toLowerCase() + ".txt", true)); 
     PrintWriter outputFileArrival = new PrintWriter(new FileWriter(arrival.toLowerCase() + ".txt", true)); 

     minimumInputs[airportSelectionOne()] = minimumInputs[airportSelectionOne()] + flightSeatingAmount(); 
     minimumInputs[airportSelectionTwo()] = minimumInputs[airportSelectionTwo()] + flightSeatingAmount(); 
     int day = (int)(Math.random() * 21); 

     int flightHours = flightTimeIntervalHours(); 
     int flightMinutes = flightTimeIntervalHours(); 

     for (int i = 0; i <= flightSeatingAmount(); i++) { 
      String passengersName = nameGenerator(); 
      int arrivalHour = (flightHours + flightDuration()); 

      if(arrivalHour > 24) { 
       arrivalHour = arrivalHour - 24; 
      } 

      outputFileDeparture.println(passengersName + "," + "0" + "," + departure + "," + String.format("%02d", day) + flightHours + flightMinutes); 
      outputFileArrival.println(passengersName + "," + "1" + "," + arrival + "," + String.format("%02d", day) + (flightHours + flightDuration()) + flightMinutes); 
      //System.out.println(count); 
      //count++; 
     } 

     outputFileDeparture.close(); 
     outputFileArrival.close(); 
    } 

} 
+0

該代碼是否編譯? – Baby

+0

是的,當我編譯程序完美運行時,它保持運行,並且每個文件最終都有超過100,000行。 – user1881401

+0

我建議你在[代碼評論](http://codereview.stackexchange.com/)上發佈此代碼。 – Baby

回答

0

我沒有足夠的代表處發表評論尚未:(​​

你不能使用do-while循環?你會覆蓋的最小整數數學方法和修改方法,以便它會適合你的喜好。然後做同樣的最大數量。然後說,而(NUM>分鐘& & <最大)

我不知道這是否是100%可信,但它可能會工作...

+0

不,它沒有工作,我已經試過了。 – user1881401