我正在做一个分配,该分配正在制作一个程序,该程序通过2d数组显示添加和减去飞机上的乘客。我正在尝试编写代码,以检查乘客要求的座位是否已预留或打开。我进行了循环遍历2d数组的循环,这些循环检查2d数组的元素是否具有我所创建的乘客名称数组的匹配元素。如果乘客要求的座位上有名字,程序应打印Denied
;如果座位打开,程序应打印Confirmed
。现在,我的程序“正确”地执行了此操作,但是多次显示Denied
或Confirmed
。我不确定如何解决此问题。我怀疑这是因为我的break;
语句仅切断了该语句所属的最内层循环。
这是我所有的代码(某些部分尚未完成,但是不会干扰我的工作):
import java.util.Scanner;
public class Plane {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
chartMaker();
} //ends main
public static void chartMaker() {
String operator = " ";
String Pname = " ";
String SeatNum = " ";
int runtime = 0;
int SeatsTaken = 0;
int TotalSeats = 48;
int SeatsAvailable = 0;
String[][] chart = new String[][]{
{"1", "2", "3", "x", "4", "5", "6"},
{"7", "8", "9", "x", "10", "11", "12"},
{"13", "14", "15", "x", "16", "17", "18"},
{"19", "20", "21", "x", "22", "23", "24"},
{"25", "26", "27", "x", "28", "29", "30"},
{"31", "32", "33", "x", "34", "35", "36"},
{"37", "38", "39", "x", "40", "41", "42"},
{"43", "44", "45", "x", "46", "47", "48"}
};
for(int i = 0; i < chart.length; i++) //starts printing of chart
{
for(int j = 0; j < chart[i].length; j++)
{
System.out.print(chart[i][j] + " ");
}
System.out.println();
} //ends printing of chart
System.out.println();
while(runtime < 48) {
System.out.print("Enter + to add a passenger, - to delete a passenger, or q to quit: ");
operator = sc.nextLine();
switch(operator) {
case "+":
System.out.print("Enter passenger name: ");
Pname = sc.nextLine();
System.out.print("Enter seat number: ");
SeatNum = sc.nextLine();
SeatChance(Pname, SeatNum, SeatsTaken, TotalSeats, SeatsAvailable);
SeatsTaken++;
for(int a = 0; a < chart.length; a++) //prints the updated chart
{
for(int b = 0; b < chart[a].length; b++)
{
if(SeatNum.contentEquals(chart[a][b]))
{
chart[a][b] = Pname;
System.out.print(chart[a][b] + " ");
}else {
System.out.print(chart[a][b] + " ");
}
}System.out.println();
} //ends printing the updated chart
System.out.println("Passenger: " + Pname);
System.out.println("Seat Number: " + SeatNum);
String[] Passengers = new String[SeatsTaken];
Passengers[SeatsTaken - 1] = Pname;
System.out.println("Passengers: ");
for(int j = 0; j < Passengers.length; j++)
{
System.out.println(Passengers[j]);
}
for(int x = 0; x < chart.length; x++) //checks if seat is taken
{
for(int c = 0; c < chart[x].length; c++)
{
for(int g = 0; g < Passengers.length; g++)
{
if(chart[x][c].contentEquals(Passengers[g]))
{
System.out.println("Denied");
break;
}else {
System.out.println("Confirmed");
break;
}
}
}
}
break;
case "-":
System.out.println("");
break;
case "q":
runtime = runtime + 48;
System.out.println("End of Program");
break;
} //ends switch statement
runtime++;
} //ends while loop for asking user
/*UpdateChart(chart, Pname, SeatNum);
*/
} //ends chartMaker
//seatChance is broken
public static void SeatChance(String Pname, String SeatNum, int SeatsTaken, int TotalSeats, int SeatsAvailable) {
double SeatChance = 0.0;
SeatsAvailable = TotalSeats - SeatsTaken;
SeatChance = (SeatsAvailable / TotalSeats) * 100;
System.out.println(Pname + " has a " + SeatChance + "% chance of getting the seat");
} //ends SeatChance
/*
public static void UpdateChart(String[][] chart, String Pname, String SeatNum) {
for(int a = 0; a < chart.length; a++)
{
for(int b = 0; b < chart[a].length; b++)
{
if(SeatNum.equals(chart[a][b]))
{
System.out.print(Pname);
}else {
System.out.print(chart[a][b]);
}
}
System.out.println();
}
} //ends UpdateChart
*/
} //ends Plane
这里是我的代码,需要修复:
for(int x = 0; x < chart.length; x++) //checks if seat is taken
{
for(int c = 0; c < chart[x].length; c++)
{
for(int g = 0; g < Passengers.length; g++)
{
if(chart[x][c].contentEquals(Passengers[g]))
{
System.out.println("Denied");
break;
}else {
System.out.println("Confirmed");
break;
}
}
}
}
这是我的输出:
Passenger: John
Seat Number: 14
Passengers:
John
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Denied
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
Confirmed
我假设我的2d数组更新数组中元素的值时会在输出中产生随机Denied
,但我不确定。
break
语句退出该块,此外,确保您所在的'for'或'while'循环不会重复。