Java for循环一遍又一遍地重复
这是我的程序中唯一不能正常工作的部分。如果这个人选择了一个50美元的座位,它会打印出一个0来标记它已经售出。如果不再有那个价格的座位,它应该打印“对不起,那个座位不可用。”它确实会打印,但会打印8次。(50美元的座位数)这是我的全部密码Java for循环一遍又一遍地重复,java,loops,if-statement,Java,Loops,If Statement,这是我的程序中唯一不能正常工作的部分。如果这个人选择了一个50美元的座位,它会打印出一个0来标记它已经售出。如果不再有那个价格的座位,它应该打印“对不起,那个座位不可用。”它确实会打印,但会打印8次。(50美元的座位数)这是我的全部密码 import java.util.*; public class Theater { /** * @param args the command line arguments */ static int[][] seats
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.");
}
}
}
import java.util.*;
公营剧院{
/**
*@param指定命令行参数
*/
静态int[]seatsArray;
静态扫描仪输入=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
seatsArray=newint[][]{
{ 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(“欢迎来到竞技场”);
System.out.println(“第1行,第1列,是前一行的底部”);
System.out.println(“零意味着座位售出”);
座位(座位排);
字符n='Y';
而((n='Y')| |(n='Y'))
{
System.out.println(“按座位s或价格p选择:”);
System.out.println(“(键入Q以取消”);
char seat=input.next().charAt(0);
开关(座椅)
{
案例:案例:
{
编号(seatsArray);
打破
}
案例'P':案例'P':
{
价格(海运航线);
打破
}
案例“Q”:案例“Q”:
{
System.out.print(“感谢您选择竞技场”);
系统出口(0);
}
违约:
{
System.out.println(“错误:请重试。”);
}
}
System.out.print(“您想预订另一个座位吗?”:”;
n=input.next().charAt(0);
}
System.out.print(“感谢您选择竞技场”);
}
公共静态空座位(国际海运阵列[])
{
对于(int[]seatsArray 1:seatsArray)
{
对于(int j=0;j0)
系统输出打印(“\t”);
系统输出打印(seatsArray1[j]);
}
System.out.println();
}
}
公共静态无效价格(国际海运阵列[])
{
System.out.print(“请输入您想要的座位的价格:”;
int price=input.nextInt();
//布尔值=false;
out:for(int i=0;i问题发生在价格方法而不是数字上,如果用户输入50
,您将检查矩阵seatsArray[][]
,查看可用座位:
解决方案:当座位被预订时,您需要添加一个布尔值
,这样您就可以将其设置为真
,并跳出的循环,而不是if
“if循环反复”
——没有“if循环”这样的事情。请显示您使用此方法的代码。好的,您的for循环不断重复--但其代码在哪里?在得到答案后,请不要对您的问题进行太大的更改。尤其不赞成删除所有代码。@JohannFranz you welcome:),如果有效,您可以标记为aswer吗