我的2d阵列有一个问题,它没有正确输出,所以程序运行正常,但是当我选择案例4查看水平座位图水平输出到许多x而不是正确的位置。如果有人能指出我的方向会很棒!
public static void viewSeatingChartVertical(boolean seat[]){
for(int i = 0; i < 10; ++i){
for(int j = 0; j < 3; ++j){
if(seat[((j + 1) + (i * 3)) - 1])
System.out.print("x ");
else
System.out.print("o ");
}
System.out.println();
}
}
public static void viewSeatingChartHorizontal(boolean seat[]){
int [][] twoDim = new int [3][10];
int a = (twoDim.length);
int b = (twoDim[0].length);
for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++) {
int x = 0;
twoDim[i][j] = x;
if(seat[((j + 1) + (i * 3)) - 1])
System.out.print("x ");
else
System.out.print("o ");
}
System.out.println();
}
}
public static void main(String args[]){
java.util.Scanner input = new java.util.Scanner(System.in);// if the system doesnt know the Scanner function it then looks to import it
boolean seating[] = new boolean[30];//declare the amount of seats available on the plane in an array
//display list of options - saving space make easier to read on smaller screen
System.out.println("Please choose an option:");
System.out.println("1 for “first class”");
System.out.println("2 for “economy”");
System.out.println("3 to view seating chart");
System.out.println("4 to view seating horizontally chart");
System.out.println("0 to exit");
System.out.print("? ");
while(true){ //loop while valid
int mOpt = input.nextInt(); //mOpt Menu Option - validate number entered, must be 0-4
switch (mOpt){
case 0: System.exit(0);
break; //system exit
case 1: // first class seats
{
System.out.print("Which seat would you like (1-9)\n");
int fcseat = input.nextInt();
if(fcseat > 0 && fcseat <10){
if(seating[fcseat - 1]){
System.out.print("That seat is taken.\n");
}
else{
seating[fcseat - 1] = true;
System.out.print("Seat number " + fcseat + " was assigned.\n");
}
}
}
break;
case 2: // economic seats
{
System.out.print("Which seat would you like (10-30)\n");
int econSeat = input.nextInt();
if(econSeat >= 10 && econSeat <= 30){ // HAD 31 NOT 30. SMH
if(seating[econSeat - 1]){
System.out.print("That seat is taken.\n");
}
else{
seating[econSeat - 1] = true;
System.out.print("Seat number " + econSeat + " was assigned.\n");
}
}
}
break;
case 3: //printout of available seats vertically
{
viewSeatingChartVertical(seating);
}
break;
case 4: //printout of available seats horizontally
{
viewSeatingChartHorizontal(seating);
}
break;
default: //wrong format or number please try again
System.out.print("Sorry, Option not recognized, please Try again.\n");
}
}
}
}
答案 0 :(得分:0)
您正在混合平面上的行和列,屏幕上有行和列。
你的横向输出真的太复杂了。它应该与垂直输出一样简单。关键是复制垂直输出中的代码,但反转两个for
语句。
for(int j = 0; j < 3; ++j) {
for(int i = 0; i < 10; ++i) {
它应该神奇地工作。
好吧,差不多工作了。这将翻转飞机的左右两侧。对第一个循环的一个小改动也将解决这个问题。留给学生。