Нахождение первого элемента в массиве, который совпадает с пользовательским вводом, и печать этого местоположения между двумя двумерными массивами - PullRequest
0 голосов
/ 14 апреля 2019

Использование двух двумерных массивов, представляющих схему размещения кинотеатров.Используйте 3 метода, которые принимают пользовательский ввод с запросом цены места, который ищет один массив, затем находит первое место, которое появляется во втором массиве.Моя проблема в том, что если «10» является входом, который является правильным входом.Номер места, который должен быть найден первым, должен быть равен 1. Вместо этого он находит место 51. Проблема может быть в моем втором методе, который ищет массив цен и сохраняет местоположение для передачи третьему методу.

Я попытался распечататьпоиск, чтобы увидеть, если есть проблема, кажется, не читать каждую строку и пропускает столбцы в массиве цен.Я пробовал разные значения для того, где начинается поиск.

 import java.util.Scanner;
    public class MovieSeating
    {
     public static void main(String[] args)
     {
       //Seating arrangment for movie theater
       System.out.println("Seating Arrangement");
       int seat = 1;       
       int[][] seating = new int[9][10];
       for(int i = 0;i < seating.length;i++){
         for(int j = 0; j < seating[0].length;j++){
           seating[i][j] = seat;
           seat = seat + 1;
         }
       }
       for(int i = 0;i < seating.length;i++){
         for(int j = 0; j < seating[0].length;j++){
           System.out.print(seating[i][j] + " ");
         }
         System.out.println("");
       }

       System.out.println();
       //seat pricing array for movie theater
       System.out.println("Seat Ticket Price");
       int[][] pricing = 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, 40, 50, 50, 50, 50, 50, 50, 40, 20},
         {80, 50, 50, 80, 80, 80, 80, 50, 50, 30}
      };


       for(int i = 0;i < pricing.length;i++){
         for(int j = 0; j < pricing[0].length;j++){
           System.out.print(pricing[i][j] + " ");
         }
         System.out.println();
       }

         System.out.println();
         //Calling method 1
         pricing(pricing, seating);
       }

       //Method 1
       // validation and termination
       //arguements take the 2 arrays
       //Method performs user input searching for seat pricing and conducts validation
       public static void pricing(int[][] pricing, int[][] seating)
       {
         Scanner in = new Scanner(System.in);
         int price = 0;
         while(true){
           System.out.println("Please pick price or press Q to quit");
           if(in.hasNextInt()){                                              
  //checks to see if it is valid input 
             price = in.nextInt();                                           
  //enter price
           }else{ 
             String garbage = in.next();                                     
  //Bad input catch
             if(garbage.equals("Q") || garbage.equals("q")){                 
  //Termination method
               System.out.println("Thanks for Checking.... Good bye.");
               break;
             }
           }
           if(price == 10 || price == 20 || price == 30 || price == 40 || price == 50 || price == 80){
             //calling method 2
             available(pricing, seating, price);
           }else{         
             System.out.println("Please pick a valid price. Valid prices are $10, $20, $30, $40, $50, and $80");
           }
           price = 0;
         }                          
       }
       //Method 2
       //arguements take both arrays again and the user price input
       //searches through both arrays to determine seating arrangements

       public static void available(int[][] pricing, int[][] seating, int input)
       {

          System.out.println("Checking for availability...");               
  //If not Q it will check to see if price is found or invalid by searching array
           int check = 0;
           int arraycheck1 = 0;   //helps record location of price
           int arraycheck2 = 0;   //helps record location of price
           for(int i = 0;i < pricing.length; i++){
             for(int j = 0; j < pricing[0].length;j++){
               if(pricing[i][j] == input){             
                 arraycheck1 = i;                          
                 arraycheck2 = j;
                 pricing[i][j] = 0;   // converts that price to zero so it will represent being "sold"
                 check = 1;  
                 break;
               }
             }
           }
           int seat = seating[arraycheck1][arraycheck2];   // finds seat number with the price location

           //calling method 3
           confirmation(check, seat);            
           check = 0;   

       }
       //Method 3
       // Determines the print statement for confirmation
       public static void confirmation(int valid, int seat)
       {

       //If the price is not valid then it prints not found
       if(valid == 0){
       System.out.println("No seat at this price is available. Sorry!");
       }
       //if the input price was valid it prints the seat confirmation
       if(valid == 1){
         System.out.println("Your seat is Confirmed! Your seat number is " + seat + " Enjoy your movie");
       }

     }   
   }

, если введено 10.В нем будет указано место подтверждено, а ваш номер места равен 1. Если вы введете 10 еще раз, будет указано место 2.

1 Ответ

0 голосов
/ 15 апреля 2019

Проблема связана с методом 2 для цикла.Присвойте первому циклу "external: for (" и добавьте "break external" для условия разрыва во втором цикле.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...