2016-11-12 44 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这样做可以防止你犯这个错误你做

相关问题