我是一年级学生,在学校学习二传手和吸气。 当我运行它时,它忽略了以下语句:
String Origin = scan.nextLine();
然后它转到下一行。
这是主要的:
import java.util.Scanner;
public class FlightTest
{
public static void main (String [] args)
{
Scanner scan = new Scanner (System.in);
Flight Flight1 = new Flight ();
System.out.print ("Airline Name: ");
String AirlineName = scan.nextLine ();
System.out.print ("Flight Number: ");
int FlightNumber = scan.nextInt ();
System.out.print ("Origin: ");
String Origin = scan.nextLine(); // There is a problem here
System.out.print ("Destination: ");
String Destination = scan.nextLine ();
}
}
这是班级
public class Flight
{
private String AirlineName;
private int FlightNumber;
private String Origin;
private String Destination;
public String setAirlineName()
{
String Name = AirlineName;
return Name;
}
public String getAirlineName()
{
return AirlineName;
}
public int setFlightNumber ()
{
int Number = FlightNumber;
return Number;
}
public int getFlightNumber ()
{
return FlightNumber;
}
public String setOrigin ()
{
String Orig = Origin;
return Orig;
}
public String getOrigin ()
{
return Origin;
}
public String setDestination ()
{
String Desti = Destination;
return Desti;
}
public String getDestination ()
{
return Destination;
}
public String toString ()
{
String result = AirlineName + " flight number "
+ FlightNumber + " leaves from " + Origin + " to "
+ Destination + ".";
return result;
}
}
答案 0 :(得分:2)
因为scan.nextInt()
不使用整行,下一次调用scan.nextLine()
会返回航班号所在的行的末尾,这可能只是换行符。
再次致电scan.nextLine()
System.out.print ("Flight Number: ");
int FlightNumber = scan.nextInt ();
scan.nextLine(); // get rid of rest of line
System.out.print ("Origin: ");
String Origin = scan.nextLine();
我觉得事情会奏效。
答案 1 :(得分:0)
你的setter错了,你应该传递一个参数。
private String airlineName;
public String getAirlineName() {
return airlineName;
}
public void setAirlineName(String airlineName) {
this.airlineName = airlineName;
}
你可以像这样使用setter
Flight f = new Flight ();
System.out.print ("Airline Name: ");
f.setAirlineName(scan.nextLine ());