![](/img/trans.png)
[英]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.