Welcome to Surinam Airlines - We may get you there!
We have 6 seats available for flight JAVA123.
How many seats do you want to book? 3
Enter passenger #1's name: Taylor Swift
Enter passenger #1's food option
(V = vegetarian, N = no preference, H = Halal): V
Enter passenger #1's class (1 = first, 2 = business, 3 = economy): 1
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown source)
at Passenger.enterPassenger(Passenger.java:64)
at RunAirLine.main(RunAirLine.java:23)
这似乎与对象没有被实例化有关,尽管我不太明白为什么。有人知道出什么事了吗?
import java.util.*;
public class RunAirLine {
private static Passenger seatArray[][] = new Passenger[2][3]; // declares new
// array of
// Passengers
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Surinam Airlines – We may get you there!");
System.out.println();
int seats = 6; // available seats
while (seats > 0) {
System.out.println("We have " + seats
+ " seats available for flight JAVA123.");
System.out.println();
System.out.print("How many seats do you want to book? ");
int n = kb.nextInt();
System.out.println();
for (int i = 0; i < n; i++) {
int x = emptySeat()[0];
int y = emptySeat()[1];
// retrieves info from emptySeat() function
seatArray[x][y] = new Passenger();
seatArray[x][y].enterPassenger(7 - seats); // gives the method the nth
// passenger number
seats--; // takes away 1 from the available seats
}
System.out.println();
}
System.out.println("No seats available.");
System.out.println();
System.out.println("Flight manifest:");
System.out.println();
for (int y = 0; y < 3; y++) { // prints out the list of flyers
for (int x = 0; x < 2; x++) {
System.out.println(seatArray[x][y].getSeat() + " "
+ seatArray[x][y].printPassengerCode());
}
}
kb.close();
}
public static int[] emptySeat() {
for (int y = 0; y < 3; y++) {// finds the next empty seat
for (int x = 0; x < 2; x++) {
if (seatArray[x][y] == null || seatArray[x][y].getSeat().equals("")) {
// if the spot hasn't been declared yet or is empty
return new int[] { x, y };
}
}
}
return null; // if none found then return null
}
}
import java.util.*;
public class Passenger {
private String name;
private String seat;
private char foodOption;
private int seatClass;
// reduces some redundancy
private String classStr;
private String prefStr;
public Passenger() { // constructors
name = "";
seat = "";
foodOption = ' ';
seatClass = -1;
}
public Passenger(String n, String s, char f, int sc) { // assigns values
// according to input
name = n;
seat = s;
foodOption = f;
seatClass = sc;
}
// get and set methods
public void setName(String n) {
name = n;
}
public void setSeat(String s) {
seat = s;
}
public void setFoodOption(char f) {
foodOption = f;
}
public void setSeatClass(int sc) {
seatClass = sc;
}
public String getName() {
return name;
}
public String getSeat() {
return seat;
}
public char getFoodOption() {
return foodOption;
}
public int getSeatClass() {
return seatClass;
}
// end of get and set methods
public void enterPassenger(int i) {
Scanner kb = new Scanner(System.in);
// asks the important questions
System.out.print("Enter passenger #" + i + "’s name: ");
name = kb.nextLine();
System.out.println("Enter passenger #" + i + "’s food option ");
System.out.print("(V = vegetarian, N = no preference, H = Halal): ");
foodOption = kb.nextLine().toUpperCase().charAt(0);
System.out.print("Enter passenger #" + i
+ "’s class (1 = first, 2 = business, 3 = economy): ");
seatClass = kb.nextInt();
System.out.println();
char seatChar = 'A'; // calculates the seat number
if (i % 2 == 0)
seatChar = 'B';
seat = Integer.toString(i / 2 + i % 2) + seatChar;
classStr = "Economy Class"; // transfers the class choice int into string
switch (seatClass) {
case 1:
classStr = "First Class";
break;
case 2:
classStr = "Business Class";
break;
}
prefStr = "No preference"; // transfers the preference char into string
switch (foodOption) {
case 'V':
prefStr = "Vegetarian";
break;
case 'H':
prefStr = "Halal";
break;
}
System.out.println("Done – Seat " + seat + " booked");
System.out
.println("-------------------------------------------------------------------");
System.out.println(name + "(" + classStr + " - Seat " + seat + ") - "
+ prefStr);
System.out
.println("-------------------------------------------------------------------");
kb.close();
}
public void printTicketStub() {
char initial = name.split(" ")[0].toUpperCase().charAt(0); // splits name
// into first
// name and
// second name
System.out.println(initial + ". " + name.split(" ")[1] + "(" + classStr
+ " - Seat " + seat + ") - " + prefStr);
}
public String printPassengerCode() { // forms the code as required
return seatClass + name.toUpperCase().substring(name.length() - 4)
+ foodOption;
}
}
您有两个问题:
>
为system.in
创建扫描程序
两次。你应该只做一次。我已经注释掉了在passenger.java
中创建的引用,并将引用更改为runairline.java
中的引用:
public void enterPassenger(int i) {
// Scanner kb = new Scanner(System.in);
// asks the important questions
System.out.print("Enter passenger #" + i + "’s name: ");
name = RunAirLine.kb.nextLine();
System.out.println("Enter passenger #" + i + "’s food option ");
System.out.print("(V = vegetarian, N = no preference, H = Halal): ");
foodOption = RunAirLine.kb.nextLine().toUpperCase().charAt(0);
System.out.print("Enter passenger #" + i
+ "’s class (1 = first, 2 = business, 3 = economy): ");
seatClass = RunAirLine.kb.nextInt();
RunAirLine.kb.nextLine();
每次执行nextint()
时,它不会从扫描仪的读数中删除新的行,这会把事情搞砸。如果希望nextint()
位于它自己的行上,则应该在nextint()
调用之后抛出一个虚拟的runairline.kb.nextline();
。这需要执行两次,一次在上面修复的代码中,一次在runairline.class
中:
System.out.print("How many seats do you want to book? ");
int n = kb.nextInt();
kb.nextLine();
System.out.println();
我正在做一个编码练习:给定一个整数序列作为一个数组,确定是否可以通过从数组中删除不超过一个元素来获得严格递增的序列。 所以我写了这段代码: 现在,这段代码似乎适用于大多数序列,但这段代码引发了一个错误: 错误如下: 我只是不明白列表索引怎么可能超出范围…有人有线索吗?
问题内容: 我为按钮添加了图像,但是当我运行该框架时,将抛出此异常。为什么?请帮助我。 第138行: 第91行: 我使用了此检查不当(建议使用Peter Lang):System.out.println(getClass()。getResource(“ / Images / yahoo_1.gif”)); 它返回null,为什么?请帮我。 问题答案: 这意味着,将返回。 JavaDoc指出,如果
问题内容: 为什么这段代码不抛出?它在不使用方法的情况下修改了一段时间,这是唯一安全的删除方法。 如果将替换为,则会得到相同的结果。但是,如果我将列表更改为或只是得到了预期的异常。到底是怎么回事?我正在使用是否相关。 编辑 我找到了以下链接 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4902078 相关部分是 天真的解决方案是将协同修改
问题内容: 考虑以下功能: 它们应该是等效的。但是存在性能差异: 不带的版本else慢10%。这非常重要。为什么? 问题答案: 对我来说,它们的速度几乎相同:(Debian上的Python 2.6.6) 字节码也非常相似: 唯一的区别是,如果控制到达函数主体的末尾,则else返回包含代码的版本None。
我正在尝试构建一个非循环树结构,每个节点由字符串标识,每个分支节点的类型为PrimMap。同一代上的所有节点都保存为映射中的Item对象。这是缩写代码: 错误消息是:/src/PrimMap。h: 314:57:错误:将“const PrimMap”作为“PrimMap::error PrimMap::add(const string)”的“this”参数传递 我看不出这个错误消息有什么意义——特
问题内容: 输出: 问题答案: 之所以陷入僵局,是因为结构是通过值而不是通过引用传递的。 将WaitGroup传递给函数时,需要传递 指针 而不是值。否则,将使用WaitGroup的副本。 这是您的工作示例: