我正在研究这个航空公司计划。该程序应该询问用户飞机上有多少座位被售出然后我有这个for循环以允许用户输入飞机上每个人的姓名和用餐选择。
我已经尝试阅读有关stackoverflow上无限for循环的一些不同问题,但我似乎无法弄清楚我的代码究竟出了什么问题。我觉得必须有一些关于循环的东西,我不理解,因为我认为只有你的i< someNumber不再是真的。
因此,当我运行这个程序时,说我输入2个座位,我希望它只能通过循环两次,但它只是继续前进。要一个名字然后吃饭。
import java.util.Scanner;
public class Flyers
{
String name;
String mealType;
int economySeats;
int businessSeats;
int firstClassSeats;
int count;
public Flyers()
{
}
public String toString()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
name = in.next();
System.out.print("Enter a meal: ");
mealType = in.next();
return "Name: " + name + ", Meal " + mealType;
}
public void addEconomyFlyers()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of economy seats sold: ");
economySeats = in.nextInt();
for(count = 0; count < economySeats; count++)
{
System.out.print("Enter a name: ");
name = in.next();
System.out.print("Enter a meal: ");
mealType = in.next();
Flyers newFlyer = new Flyers();
String seat = newFlyer.toString();
}
}
如果有帮助,这是我的主要课程。
import java.util.Scanner;
public class Aviation
{
public Aviation()
{
}
public static void main(String[] args)
{
int option;
FlightCost newFlight = new FlightCost();
FlightProfit flight = new FlightProfit();
Flyers newFlyers = new Flyers();
Scanner in = new Scanner(System.in);
System.out.print("Enter new flight location: ");
String location = in.next();
do{
String menu = "\n Please select an option to perform"
+ "\n1 (1) Get flight costs."
+ "\n2 (2) Get flight profits."
+ "\n3 (3) Enter names/meals."
+ "\n4 (4) Exit.";
System.out.println(menu);
option = in.nextInt();
}while (option < 0 || option > 4);
switch(option)
{
case 1:
newFlight.getCost(location);
break;
case 2:
flight.addEconomySeats();
flight.addBusinessSeats();
flight.addFirstClassSeats();
flight.getProfit(location);
break;
case 3:
newFlyers.addEconomyFlyers();
break;
case 4:
System.out.println("Exit");
break;
default:
System.out.println("Error: must select menu option.");
}
}
}
答案 0 :(得分:0)
从toString()
中删除以下代码 Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
name = in.next();
System.out.print("Enter a meal: ");
mealType = in.next();
您已经在for
循环中阅读了这些内容。
此外,您的do-while
循环完全错误。这个条件没有意义。它应该是option > 0 && option <4
答案 1 :(得分:0)
帕特说:
Scanner in = new Scanner(System.in);
System.out.print("Enter a name: ");
name = in.next();
System.out.print("Enter a meal: ");
mealType = in.next();
toString()中的这段代码已经过时了
然而do while确实有意义,至少对我来说,因为我看到你希望每次用户给出无效的int时打印出菜单,但是你仍然让0通过虽然这不是一个有效的选择,do while应该是:
while (option <= 0 || option > 4);
或
while (option < 1 || option > 4);
代码中的另一个怪癖:
System.out.print("Enter a name: ");
name = in.next();
System.out.print("Enter a meal: ");
mealType = in.next();
Flyers newFlyer = new Flyers();
String seat = newFlyer.toString();
你创建了一个你刚刚修改过的类的新实例并且调用了toString(),即使你没有用数据填充该类,所以在建议的编辑pat之后的toString方法会返回&# 34;姓名:用餐&#34;
正如我看到您的代码正在进行中,尝试在各种变量上添加更多System.out.print以用于调试目的或使用调试器,虽然我知道在学习时使用调试器很困难,但它确实适合我。还要记住,每次创建新的实例ex:
Flyers newFlyer = new Flyers();
它只有默认设置的数据或在构造函数ex:
中设置的数据public Flyers()
{
}
我希望我写的内容很有帮助,祝你有个美好的一天,并坚持下去。