[英]JSON File Creation using for each loop And Map - It repeats the first element value on all iterations over for loop
[英]for loop repeats over and over
這是我的程序中唯一不能正常工作的部分。 如果這個人選擇了一個 50 美元的座位,它會打印一個 0 來標記它已經售出。 如果不再有該價格的座位,則應打印出“抱歉,該座位不可用”。 它確實打印了那個,但它打印了 8 次。 (50 美元座位的數量)這是我的全部代碼。
import java.util.*;
public class Theater {
/**
* @param args the command line arguments
*/
static int[][] seatsArray;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
seatsArray = new int[][] {
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },
{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
{ 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 }};
System.out.println("Welcome to The Arena.");
System.out.println("row 1, column 1, is the bottom front row.");
System.out.println("A zero meanss tha seat is sold.");
seats(seatsArray);
char n = 'Y';
while ((n == 'Y') || (n == 'y'))
{
System.out.println("Pick by Seat s or by Price p: ");
System.out.println("(Type Q to cancel.)");
char seat = input.next().charAt(0);
switch (seat)
{
case'S':case's':
{
number(seatsArray);
break;
}
case'P':case'p':
{
price(seatsArray);
break;
}
case'Q':case'q':
{
System.out.print("Thank you for choosing The Arena");
System.exit(0);
}
default:
{
System.out.println("Error: Please Try Again.");
}
}
System.out.print("Would you like to reserve another seat (Y/N)?: ");
n = input.next().charAt(0);
}
System.out.print("Thank you for choosing the Arena.");
}
public static void seats(int seatsArray[][])
{
for (int[] seatsArray1 : seatsArray)
{
for (int j = 0; j < seatsArray1.length; j++)
{
if (j>0)
System.out.print("\t");
System.out.print(seatsArray1[j]);
}
System.out.println();
}
}
public static void price(int seatsArray[][])
{
System.out.print("Please enter a price for the seat you would like: ");
int price = input.nextInt();
// boolean found = false;
out: for (int i=0;i<9;i++)
for (int j=0;j<10;j++)
if (seatsArray[i][j]==price)
{
seatsArray[i][j] = 0;
seats(seatsArray);
System.out.println("Your seat has been reserved. A zero will show in the seat you bought.");break out;
}
else if(seatsArray[i][j]== 0)
{
System.out.println("Sorry, that seat is unavailable.");
}
}
public static void number(int seatsArray[][])
{
System.out.println("Enter a row, then enter a seat number.");
System.out.print("What row would you like to sit on?:");
int i = input.nextInt();
i = Math.abs(i-9);
System.out.print("What seat of that row do you want?:");
int j = input.nextInt();
j -= 1;
if (seatsArray[i][j]!=0)
{
seatsArray[i][j] = 0;
seats(seatsArray);
System.out.println("Your seat has been reserved. A zero will show in the seat you bought.");
}
else
{
System.out.println("Sorry, that seat is unavailable.");
}
}
}
這是輸出:
您的座位已被預訂。 您購買的座位上會顯示零。 您想預訂另一個座位嗎(是/否)?: y 按座位 s 或按價格選擇 p:(鍵入 Q 取消。) p 請輸入您想要的座位的價格:50 抱歉,該座位不可用. 抱歉,該座位不可用。 抱歉,該座位不可用。 抱歉,該座位不可用。 抱歉,該座位不可用。 抱歉,該座位不可用。 抱歉,該座位不可用。 抱歉,該座位不可用。 您想預訂另一個座位嗎(是/否)?:
問題出現在價格方法而不是數字上,如果用戶輸入50
,則您正在檢查可用座位的矩陣seatsArray[][]
:
解決方案:您需要在預訂座位時添加一個boolean
,以便您可以將其設置為true
並轉義for
而不是if
上的循環
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.