2016-11-12 45 views
-8

由於某些原因,當我在eclipse上運行我的代碼時,屏幕上沒有顯示任何內容。我不確定它是什麼。我可以在控制檯中輸入輸入,但這是關於它的。沒有其他事情發生誰能告訴我它可能是什麼?謝謝。我的java代碼沒有錯誤,但它爲什麼不運行?

import java.util.Scanner; 

public class Test { 

public static void main (String [] args) { 
    Scanner input = new Scanner (System.in); 

    Flight flight = new Flight (input.nextLine(), input.nextLine(), input.nextLine(), input.nextLine(), 0); 

    System.out.println("Creating first flight"); 
    System.out.println("What is the name of the flight?"); 
     String flightName = input.nextLine(); 
    System.out.println("What is the destination of the flight?"); 
     String destination = input.nextLine(); 
    System.out.println("What is the departure time of the flight?"); 
     String departureTime = input.nextLine(); 
    System.out.println("What is the departure gate of the flight?"); 
     String departureGate = input.nextLine(); 

    boolean done = false; 
    while (!done) { 
     System.out.println("Now what would you like to do?"); 
     System.out.print("1. Print out a flight's info"); 
     System.out.print("2. Print out the number of flights through the static variable."); 
     System.out.print("3. Change the departure time of a flight."); 
     System.out.print("4. Change the departure gate of a flight."); 
     System.out.print("5. Exit"); 
     int choice = input.nextInt(); 

     switch (choice) { 

     case 1: 
      System.out.println("Which flight would you like to print the info of (1 or 2)?"); 
       int selection = 0; 
       selection = input.nextInt(); 
      if (selection == 1 || selection == 2) { 
       Flight.printFlight(); 
      } else{ 
       System.out.println("Invalid Option"); 
      } break; 

     case 2: 
      System.out.println("This is the number of flights" + Flight.getNumFlights()); 
      break; 

     case 3: 
      System.out.println("Which flight would you like to change the departure time of (1 or 2)?"); 
       int selection2 = 0; 
       selection2 = input.nextInt(); 
      if (selection2 == 1 || selection2 == 2){ 
       System.out.println("What is the new departure time for flight " + (Flight.getNumFlights()-1)); 
        String newDeptTime = input.nextLine(); 
        Flight.changeDeptTime(newDeptTime); 
      } else{ 
       System.out.println("Invalid Option"); 
      } break; 

     case 4: 
      System.out.println("Which flight would you like to change the departure gate of?"); 
       int selection3 = input.nextInt(); 
      if (selection3 == 1 || selection3 == 2){ 
       System.out.println("What is the new departure gate for flight " + Flight.getNumFlights()); 
        String newDeptGate = input.nextLine(); 
        Flight.changeDeptGate(newDeptGate); 
      } else { 
       System.out.println("Invalid option"); 
      } break; 

     case 5: 
      done = true; 
      break; 
     default: 
      System.out.println("Invalid option"); 
      break; 

     } 

    } 

} 

} 
+5

放在一個斷點,並在調試運行,並通過您的代碼步,看看它打破用戶。 – Araymer

+2

即使在輸入4行的'Flight'構造函數的參數後,您也沒有獲得輸出,對吧? – MikeCAT

+4

由於您在構建Flight時請求使用Scanner在標準輸入上輸入內容,因此在您輸入文本之前不會顯示任何內容。 –

回答

0

它不顯示任何,因爲它在等待你輸入你的飛行類的構造函數想要的字符。
,你可以在這裏看到

Flight flight = new Flight (input.nextLine(), input.nextLine(), input.nextLine(), input.nextLine(), 0); 

執行在input.nextLine()部分停止,直到你輸入一些東西,按下回車鍵

PS:它能夠更好地放置一些System.out.printlns(「輸入值」)輸入部分前一種說法,以顯示如何do.funny這樣做可以防止你犯這個錯誤你做

相關問題