我正在尝试创建一个生成机票的程序,然后询问用户是否要查看乘客列表。
我正在努力的是,在每个输入了详细信息的用户之后,他们必须被添加到乘客列表中。 因此,如果User1输入他的详细信息并且必须询问他们是否想要查看乘客列表(到目前为止乘客列表下只有一条记录),那么当用户2输入他的详细信息时,它必须询问用户是否要查看乘客列表和User2详细信息必须添加到乘客列表中(因此我们将在乘客列表下方有2条记录)等等。
请协助。
public class Question12 {
public static void main(String[] args) {
double random = Math.random()* 69 + 1;
String q;
Scanner in = new Scanner(System.in);
String[][] PlaneTicket = new String[3][3];
for (int row = 0; row < 67; row++) {
System.out.print("Enter name:");
PlaneTicket[row][0] = in.next();
System.out.print("Enter surname: ");
PlaneTicket[row][1] = in.next();
System.out.print("Enter a ID: ");
PlaneTicket[row][2] = in.next();
System.out.println("************Airport**********");
System.out.println("Seat number: " + Math.round(random));
System.out.println("Name: " + PlaneTicket[row][0]);
System.out.println("Surname: " + PlaneTicket[row][1]);
System.out.println("ID: " + PlaneTicket[row][2]);
System.out.println("*****************************");
System.out.println("Do you want to view the passanger list: " + "(Yes/No)");
q = in.next();
if ("Yes".equals(q)) {
System.out.println("Name Surname ID Seat Number");
System.out.println("----------------------------------------");
System.out.println(PlaneTicket[row][0]+" "+PlaneTicket[row][1]+" "+PlaneTicket[row][2]+ " "+ Math.round(random));
答案 0 :(得分:1)
如果您只想打印到目前为止输入的所有值,您可以执行以下操作:
System.out.println("Name Surname ID Seat Number");
System.out.println("----------------------------------------");
for (int k = 0; k <= row; k++) {
System.out.println(PlaneTicket[k][0] + " " + PlaneTicket[k][1] + " " + PlaneTicket[k][2] + " " + Math.round(random));
}
这会回答你的问题吗?
一些评论:
String[][] PlaneTicket = new String[67][3];
PassengerTicket
,这将有助于使你的代码更具可读性(并且更加面向对象)String.format
)Collections
,这会使您的代码更具可读性并且可能更易于维护,即https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html,https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 以下是您使用某些修补程序的主要方法,如上所述,请进行一些重新设计(请参阅我的评论):
public static void main(String[] args) {
final int maxPassengers = 67;
double random = Math.random() * 69 + 1;
String q;
Scanner in = new Scanner(System.in);
String[][] PlaneTicket = new String[maxPassengers][4];
for (int row = 0; row < maxPassengers; row++) {
System.out.print("Enter name:");
PlaneTicket[row][0] = in.next();
System.out.print("Enter surname: ");
PlaneTicket[row][1] = in.next();
System.out.print("Enter a ID: ");
PlaneTicket[row][2] = in.next();
// you should keep the SeatNumber
PlaneTicket[row][3] = String.valueOf(Math.round(random));
System.out.println("************Airport**********");
System.out.println("Seat number: " + PlaneTicket[row][3]);
System.out.println("Name: " + PlaneTicket[row][0]);
System.out.println("Surname: " + PlaneTicket[row][1]);
System.out.println("ID: " + PlaneTicket[row][2]);
System.out.println("*****************************");
System.out.println("Do you want to view the passanger list: " + "(Yes/No)");
q = in.next();
if ("Yes".equalsIgnoreCase(q) || "y".equalsIgnoreCase(q)) {
System.out.println("Name Surname ID Seat Number");
System.out.println("----------------------------------------");
for (int k = 0; k <= row; k++) {
System.out.println(PlaneTicket[k][0] + " " + PlaneTicket[k][1] + " " + PlaneTicket[k][2] + " " + PlaneTicket[row][3]);
}
}
}
}
答案 1 :(得分:0)
您需要创建一个方法displayList(String[][] seats)
,并在每个客户键入"Yes"
后调用它。此方法将具有类似的循环并显示当前列表
for (int row = 0; row < 67; row++) { ....}
答案 2 :(得分:0)
你初始化一个二维数组M =
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
,你将它运行到66 ..显然给出PlaneTicket = new String[3][3]
。最好的方法是,避免使用幻数并将它们放在IndexOutOfBounds Exception
或row
之类的变量中,然后迭代到它。
否则看起来不错。
其次,请注意术语列表和数组。您的意思是Passenger list 还是只是数组?
好像你在底部有它,但你错过了一些括号。修复索引后,它应该可以工作。