i’m having a hard time figuring out why when i run my code it skips all of the if statements and just puts invalid. i have a 2d array with values which represents a movie theater and i’m supposed to be selling tickets, the user is supposed to enter a sum of money that determines where they’ll sit, which is the most expensive seat they could afford from the selection. the seat that the person gets should change from whatever number to 0. at the end i need to print the new array with the zeroes.
this what i've tried:
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
boolean done = false;
//initial seating chart
int [] [] table =
{
{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, 20, 20, 20, 20},
{20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
{30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
{40, 40, 40, 40, 40, 40, 40, 40, 40, 40},
{40, 40, 40, 40, 40, 40, 40, 40, 40, 40},
{50, 50, 50, 50, 50, 50, 50, 50, 50, 50},
};
while (!done) {
int row; int col;
//search seating chart
for (row=0; row<9; row++) {
for (col=0; col<8; col++) {
System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");
int amount = in.nextInt();
if (table[row][col]==10 && 10<=amount && amount<20) {
table[row][col]=0;
System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row+1, col+1);
System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
if (in.hasNext("y")) {
done = false;
}
else {done = true;
}
}
else if (table[row][col]==20 && 20<=amount && amount<30) {
table[row][col]=0;
System.out.printf("Ticket located at Row %d Seat %d purchased for 20\n", row+1, col+1);
System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
if (in.hasNext("y")) {
done = false;
}
else {done = true;
}
}
else if (table[row][col]==30 && 30<=amount && amount<40) {
table[row][col]=0;
System.out.printf("Ticket located at Row %d Seat %d purchased for 30\n", row+1, col+1);
System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
if (in.hasNext("y")) {
done = false;
}
else {done = true;
}
}
else if (table[row][col]==40 && 40<=amount && amount<50) {
table[row][col]=0;
System.out.printf("Ticket located at Row %d Seat %d purchased for 40\n", row+1, col+1);
System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
if (in.hasNext("y")) {
done = false;
}
else {
done = true;
}
}
else if (table[row][col]==50 && 50<=amount) {
table[row][col]=0;
System.out.printf("Ticket located at Row %d Seat %d purchased for 50\n", row+1, col+1);
System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());
if (in.hasNext("y")) {
done = false;
}
else {done = true;
}
}
else {System.out.print("invalid");}
}}}
//final seating chart
System.out.print(table);
}
}
Aucun commentaire:
Enregistrer un commentaire