Java 例如,您根本不需要计数,因为您已经有一个填充的变量执行相同的操作。另外,将filled作为全局static变量,以便可以在main和所有其他方法中访问它。或者,您可以将其作为参数传递给方法
修正后的程序如下:Java 例如,您根本不需要计数,因为您已经有一个填充的变量执行相同的操作。另外,将filled作为全局static变量,以便可以在main和所有其他方法中访问它。或者,您可以将其作为参数传递给方法,java,airplane,Java,Airplane,修正后的程序如下: import java.util.Scanner; public class AirplaneSeating { static int filled = 0; public static void main(String[] args) { // two- dimensional array with 7 rows and 4 columns char[][] seats = new char[7][4];
import java.util.Scanner;
public class AirplaneSeating {
static int filled = 0;
public static void main(String[] args) {
// two- dimensional array with 7 rows and 4 columns
char[][] seats = new char[7][4];
for (int i = 0; i < 7; i++) {
seats[i][0] = 'A';
seats[i][1] = 'B';
seats[i][2] = 'C';
seats[i][3] = 'D';
}
String seatNumber = " ";
String q = " ";
System.out.println("Welcome to the Airplane Seating Reservation System.");
System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.");
System.out.println("Enter q to exit.");
Scanner keyboard = new Scanner(System.in);
seatNumber = keyboard.nextLine();
if (seatNumber.equals("q")) {
System.out.println("Program ended.");
System.exit(0);
}
// print seating pattern and put an X in the seatNumber location
while (filled < 28 && seatNumber.length() > 0) {
int row = seatNumber.charAt(0) - '1';
int col = seatNumber.charAt(1) - 'A';
if (row < 0 || row > 7 || col < 0 || col > 4) {
System.out.println("Input error. Enter seat to assign (e.g., '1A')," + "or q to quit.");
seatNumber = keyboard.nextLine();
if (seatNumber.equals("q")) {
System.out.println("Program ended.");
System.exit(0);
}
} else {
if (seats[row][col] != 'X') {
seats[row][col] = 'X';
filled++;
System.out.println(" ");
printSeats(seats);
}
if (filled < 28) {
System.out.println("Enter seat to assign (e.g., '1A')," + "or q to quit.");
seatNumber = keyboard.nextLine();
if (seatNumber.equals("q")) {
System.out.println("Program ended.");
System.exit(0);
}
}
}
}
}
private static void printSeats(char[][] seats) {
System.out.println("Row");
for (int i = 0; i < seats.length; i++) {
System.out
.println((i + 1) + " " + seats[i][0] + " " + seats[i][1] + " " + seats[i][2] + " " + seats[i][3]);
}
int numberOfSeatsAvailable = (28 - filled);
System.out.println("There are " + numberOfSeatsAvailable + " seats available.");
} // end main
} // end class
如有任何问题/疑问,请随时发表评论。我将行置于numberOfSeatsAvailable=28;在变量声明中,以及 可用座位数--
System.out.println(“有”+numberofseatsavable+ "有座位";; System.out.println(“”);
在后面,在座椅位置回路中放置一个X。我还在为退出部分的sentinel q工作。谢谢Arvind Avinash!我更正的程序如下:
import java.util.Scanner ;
/**
* The AirplaneSeating program asks the user for the seat they would like to reserve.
* A layout of the plane is printed and an X is placed in the reserved seat. The
* program finds if the seat is available and if the entry is valid. A sentinel of q
* ends the program.
*
* @author Quang Pham
* @version Module8, Lab 2, 4/1/20
*
* Algorithm:
*
* 1. Greet user and ask which seat they would like to reserve.
* 2. Print a layout of the plane and the seats available.
* 3. Put an X in the position where the user would like to reserve.
* 4. Loop and ask if they'd like to reserve another seat.
* 5. Make certain seat is available and the entry is valid, if sentinel q
* is entered, exit program.
*
* Problem Description:
*
* Write a program to assign passenger's seats in a small airplane. Assume the
* plane has its seats numbered as follows:
*
* Row
* 1 A B C D
* 2 A B C D
* 3 A B C D
* 4 A B C D
* 5 A B C D
* 6 A B C D
* 7 A B C D
*
* You should verify that the user enters rows between 1 and 7 only, and
* columns A, B, C, or D only. If the user enters an entry that is invalid,
* print an error message telling them what's wrong, then prompt for the next
* entry. Model the seats in the plane using a multi-dimensional array with
* seven rows and four columns. Use a loop in your program which continues to
* prompt for a seat to reserve until either the user specifies a sentinel to
* stop the program, or when all seats are reserved.
* After each entry from the user, the program should display the seat
* reservation pattern, with an 'X' marking the seats already assigned. For
* example, after seats 1A, 2B, and 4C are reserved, the display might show
* the following:
*
* Row
* 1 X B C D
* 2 A X C D
* 3 A B C D
* 4 A B X D
* 5 A B C D
* 6 A B C D
* 7 A B C D
*
* There are 25 seats available. This continues until either all seats are
* filled or the user enters a sentinel indicating that he/she is done entering
* reservations. If the user tries to reserve a seat which is already taken, the
* program should say that that seat is occupied and ask for another choice.
* Submit program files for all classes, as well as a print screen or screen
* snip showing what your screen looks like after 4 or 5 seats have been assigned.
* Be sure to demonstrate what happens when the user tries to reserve a seat that
* is already taken or specifies an invalid seat (for example, 9A or 5E).
*/
public class AirplaneSeating
{
static int filled = 0 ;
public static void main(String[] args)
{
// two- dimensional array with 7 rows and 4 columns
char[][] seats = new char [7][4] ;
for (int i = 0; i < 7; i++)
{
seats[i][0] = 'A' ;
seats[i][1] = 'B' ;
seats[i][2] = 'C' ;
seats[i][3] = 'D' ;
}
String seatNumber = " " ;
String q = " " ;
int numberOfSeatsAvailable = 28 ;
System.out.println("Welcome to the Airplane Seating Reservation System.") ;
System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.") ;
System.out.println("Enter q to exit.") ;
printSeats(seats) ;
Scanner keyboard = new Scanner(System.in) ;
seatNumber = keyboard.nextLine() ;
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
while((filled < 28) && (seatNumber.length() > 0))
{
int row = seatNumber.charAt(0) - '1' ;
int col = seatNumber.charAt(1) - 'A';
if (row < 0 || row > 7 || col < 0 || col > 4)
{
System.out.println("Input error. Enter seat to assign (e.g., '1A')," +
"or q to quit.");
seatNumber = keyboard.nextLine() ;
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
}
else
{
//put an X in the assigned seat and print seating arrangement
if (seats[row][col] != 'X')
{
seats[row][col] = 'X' ;
filled++ ;
//System.out.println(" ") ;
printSeats(seats) ;
}
if (filled < 28)
{
System.out.println("Enter seat to assign (e.g., '1A')," +
"or q to quit.");
seatNumber = keyboard.nextLine();
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
}
}
}
System.out.println("Final seat assignments: ");
printSeats(seats);
}
private static void printSeats(char[][] seats)
{
System.out.println("Row") ;
for (int i = 0; i < seats.length; i++)
{
System.out.println((i + 1) + " " +
seats[i][0] + " " + seats[i][1] + " " + seats[i][2] + " " + seats[i][3]) ;
}
int numberOfSeatsAvailable = (28 - filled);
System.out.println("There are " + numberOfSeatsAvailable + " seats available.");
System.out.println(" ");
} //end main
} //end class
import java.util.Scanner;
/**
*AirplaneSeating程序向用户询问他们想要预订的座位。
*打印飞机布局,并在预留座位上放置一个X。这个
*程序查找座位是否可用以及条目是否有效。q的哨兵
*节目结束。
*
*@作者广范
*@version Module8,实验室2,4/1/20
*
*算法:
*
* 1. 问候用户并询问他们想预订哪个座位。
* 2. 打印飞机和可用座位的布局。
* 3. 在用户希望保留的位置放置一个X。
* 4. 打个电话问他们是否想再预订一个座位。
* 5. 如果sentinel q,确保座位可用且条目有效
*进入,退出程序。
*
*问题描述:
*
*编写一个程序来分配小飞机上的乘客座位。假设
*飞机座位编号如下:
*
*划船
*1 A B C D
*2 A B C D
*3 A B C D
*4 A B C D
*5 A B C D
*6 A B C D
*7 A B C D
*
*您应该验证用户是否只输入1到7之间的行,以及
*仅A、B、C或D列。如果用户输入的条目无效,
*打印一条错误消息,告诉他们出了什么问题,然后提示进行下一步操作
*进入。使用多维数组对平面中的座椅进行建模
*七行四列。在程序中使用一个循环,该循环将继续
*提示预订座位,直到用户指定要预订的哨兵
*停止节目,或在所有座位都已预订时停止。
*每次用户输入后,程序应显示座椅
*预订模式,用“X”标记已分配的座位。对于
*例如,在保留座位1A、2B和4C后,显示屏可能会显示
*以下是:
*
*划船
*1 X B C D
*2 A X C D
*3 A B C D
*4 A B X D
*5 A B C D
*6 A B C D
*7 A B C D
*
*还有25个座位。这将一直持续到所有座位都被取消为止
*填充或用户输入一个哨兵,指示他/她已完成输入
*保留。如果用户试图预订已入座的座位,则
*程序应该说那个座位有人,并要求另一个选择。
*提交所有类的程序文件,以及打印屏幕或屏幕
*剪报显示分配4或5个座位后屏幕的外观。
*确保演示当用户试图预订一个
*已被占用或指定了无效座位(例如9A或5E)。
*/
公务舱
{
静态整数填充=0;
公共静态void main(字符串[]args)
{
//7行4列的二维数组
char[][]席位=新char[7][4];
对于(int i=0;i<7;i++)
{
席位[i][0]=“A”;
席位[i][1]=“B”;
席位[i][2]=“C”;
席位[一][3]=“D”;
}
字符串seatNumber=“”;
字符串q=“”;
int numberOfSeatsAvailable=28;
System.out.println(“欢迎使用飞机座位预订系统”);
System.out.println(“请输入您想要预订的座位(例如-1A)”;
System.out.println(“输入q以退出”);
打印座椅(座椅);
扫描仪键盘=新扫描仪(System.in);
seatNumber=keyboard.nextLine();
如果(座位号等于(“q”))
{
System.out.println(“程序结束”);
系统出口(0);
}
而((填充<28)和&(seatNumber.length()>0))
{
int row=seatNumber.charAt(0)-“1”;
int col=座位号字符(1)-“A”;
如果(行<0 | |行>7 | |列<0 | |列>4)
{
System.out.println(“输入错误。输入要分配的座位(例如,“1A”),”+
“或q退出。”);
seatNumber=keyboard.nextLine();
如果(座位号等于(“q”))
{
System.out.println(“程序结束”);
系统出口(0);
}
}
其他的
{
//在指定的座位上画一个X,然后打印座位安排
如果(座位[行][列]!='X')
{
座位[行][col]=“X”;
填充++;
//System.out.println(“”);
打印座椅(座椅);
}
如果(填充<28)
{
System.out.println(“输入要分配的座位(例如,“1A”),”+
“或q退出。”);
seatNumber=keyboard.nextLine();
如果(座位号等于(“q”))
{
System.out.println(“程序结束”);
系统出口(0);
Welcome to the Airplane Seating Reservation System.
Please enter the seat (e.g.- 1A) you wish to reserve.
Enter q to exit.
3B
Row
1 A B C D
2 A B C D
3 A X C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
There are 27 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
6A
Row
1 A B C D
2 A B C D
3 A X C D
4 A B C D
5 A B C D
6 X B C D
7 A B C D
There are 26 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
4C
Row
1 A B C D
2 A B C D
3 A X C D
4 A B X D
5 A B C D
6 X B C D
7 A B C D
There are 25 seats available.
Enter seat to assign (e.g., '1A'),or q to quit.
q
Program ended.
import java.util.Scanner ;
/**
* The AirplaneSeating program asks the user for the seat they would like to reserve.
* A layout of the plane is printed and an X is placed in the reserved seat. The
* program finds if the seat is available and if the entry is valid. A sentinel of q
* ends the program.
*
* @author Quang Pham
* @version Module8, Lab 2, 4/1/20
*
* Algorithm:
*
* 1. Greet user and ask which seat they would like to reserve.
* 2. Print a layout of the plane and the seats available.
* 3. Put an X in the position where the user would like to reserve.
* 4. Loop and ask if they'd like to reserve another seat.
* 5. Make certain seat is available and the entry is valid, if sentinel q
* is entered, exit program.
*
* Problem Description:
*
* Write a program to assign passenger's seats in a small airplane. Assume the
* plane has its seats numbered as follows:
*
* Row
* 1 A B C D
* 2 A B C D
* 3 A B C D
* 4 A B C D
* 5 A B C D
* 6 A B C D
* 7 A B C D
*
* You should verify that the user enters rows between 1 and 7 only, and
* columns A, B, C, or D only. If the user enters an entry that is invalid,
* print an error message telling them what's wrong, then prompt for the next
* entry. Model the seats in the plane using a multi-dimensional array with
* seven rows and four columns. Use a loop in your program which continues to
* prompt for a seat to reserve until either the user specifies a sentinel to
* stop the program, or when all seats are reserved.
* After each entry from the user, the program should display the seat
* reservation pattern, with an 'X' marking the seats already assigned. For
* example, after seats 1A, 2B, and 4C are reserved, the display might show
* the following:
*
* Row
* 1 X B C D
* 2 A X C D
* 3 A B C D
* 4 A B X D
* 5 A B C D
* 6 A B C D
* 7 A B C D
*
* There are 25 seats available. This continues until either all seats are
* filled or the user enters a sentinel indicating that he/she is done entering
* reservations. If the user tries to reserve a seat which is already taken, the
* program should say that that seat is occupied and ask for another choice.
* Submit program files for all classes, as well as a print screen or screen
* snip showing what your screen looks like after 4 or 5 seats have been assigned.
* Be sure to demonstrate what happens when the user tries to reserve a seat that
* is already taken or specifies an invalid seat (for example, 9A or 5E).
*/
public class AirplaneSeating
{
static int filled = 0 ;
public static void main(String[] args)
{
// two- dimensional array with 7 rows and 4 columns
char[][] seats = new char [7][4] ;
for (int i = 0; i < 7; i++)
{
seats[i][0] = 'A' ;
seats[i][1] = 'B' ;
seats[i][2] = 'C' ;
seats[i][3] = 'D' ;
}
String seatNumber = " " ;
String q = " " ;
int numberOfSeatsAvailable = 28 ;
System.out.println("Welcome to the Airplane Seating Reservation System.") ;
System.out.println("Please enter the seat (e.g.- 1A) you wish to reserve.") ;
System.out.println("Enter q to exit.") ;
printSeats(seats) ;
Scanner keyboard = new Scanner(System.in) ;
seatNumber = keyboard.nextLine() ;
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
while((filled < 28) && (seatNumber.length() > 0))
{
int row = seatNumber.charAt(0) - '1' ;
int col = seatNumber.charAt(1) - 'A';
if (row < 0 || row > 7 || col < 0 || col > 4)
{
System.out.println("Input error. Enter seat to assign (e.g., '1A')," +
"or q to quit.");
seatNumber = keyboard.nextLine() ;
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
}
else
{
//put an X in the assigned seat and print seating arrangement
if (seats[row][col] != 'X')
{
seats[row][col] = 'X' ;
filled++ ;
//System.out.println(" ") ;
printSeats(seats) ;
}
if (filled < 28)
{
System.out.println("Enter seat to assign (e.g., '1A')," +
"or q to quit.");
seatNumber = keyboard.nextLine();
if (seatNumber.equals("q"))
{
System.out.println("Program ended.") ;
System.exit(0) ;
}
}
}
}
System.out.println("Final seat assignments: ");
printSeats(seats);
}
private static void printSeats(char[][] seats)
{
System.out.println("Row") ;
for (int i = 0; i < seats.length; i++)
{
System.out.println((i + 1) + " " +
seats[i][0] + " " + seats[i][1] + " " + seats[i][2] + " " + seats[i][3]) ;
}
int numberOfSeatsAvailable = (28 - filled);
System.out.println("There are " + numberOfSeatsAvailable + " seats available.");
System.out.println(" ");
} //end main
} //end class