仅当column2已满时,数组才会超出范围

时间:2016-04-23 18:05:03

标签: java arrays runtimeexception

这是一个计划,模拟一个充满座位的剧院。最初都是可用的,因为每个都被选中,其数组值设置为-1,当迭代器看到负值时,它显示X而不是货币成本。问题在于使用了TheaterFullCheck方法。当它看到第二列是100%已满时它会崩溃......我可以看到为什么行已满可能会崩溃它但是我没有得到列......它不会那样看待它们。 任何建议将不胜感激!

    import java.util.Scanner;

public class P6point24 {

public static void main(String[] args) 
    {   
        Scanner in =new Scanner(System.in);


        final int rows = 9;
        final int columns = 10;

          int[][] seats = 
                      { { 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 },
                        { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },
                        { 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 },
                        { 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 },
                        { 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 } };



 if (theaterFullCheck(seats, rows, columns)==true) {            
 /**    int loop = 0;
int conRow = 0;
    int i=0;
    int j = 0;
while ( loop < 100)     
    {
        int elementNum = i + 1; 
        System.out.println();


        while(j < loop )
        {
**/         
                    System.out.print("Theater is SOLD OUT");
    /**             j++;
    }
        i++;
        j = 0;

    }
    System.out.println();

        **/

    } //if theater check close


else {





    while ( theaterFullCheck(seats, rows, columns)==false) //LINE 61    //Is theater full?
{
    int j = 0,    i = rows-1;

// System.out.println("generating seating");                                                // this is for debugging


    //System.out.println("Rows: " +rows);
    //System.out.println("Columns: "+columns);

    System.out.println("                                           Row");
    while (i >= 0)
    {

        //System.out.println("i is " +i);
        System.out.println("");
    while(j < columns )
    {


                if (seats[i][j]> 0)                              
                {

        System.out.print("$"+seats[i][j]+" ");                        //read each value, if positive print value with $ in front, if neg print "X" 
                j++;
                }
                else {
                    System.out.print(" X  ");
                    j++;
                }

}
    i--;
    j=0;
    System.out.print("   " + (i+2));

}//
System.out.println();
System.out.println("");
System.out.println(" 1   2   3   4   5   6   7   8   9   10 ");
System.out.println("                Column #");
System.out.println("");
System.out.println("  <    S     T     A     G     E    >    ");
System.out.println();


int selectRow = -1, selectColumn = -1;                          //- 1 from each to offset for the 0th spot in both rows and columns
String aKey=null;                                               // The only way I could find to have java PAUSE so that the purchased ticket could be viewed before refresh "press 'a key'"

System.out.println("Please input the ROW number of the seat you would like: ");
selectRow= selectRow + in.nextInt();
if (selectRow >= rows) {
    System.out.println("Error, you choose a Row which does not exist. Please try again.");
    System.out.println("Enter a key to return to Seat Selection Menu");
    aKey = in.next();                                               // enter 'a Key' to move on.
                    }
else{

System.out.println("Please input the COLUMN number of the seat you would like: ");
selectColumn= selectColumn + in.nextInt();
    if (selectColumn >= columns) {
        System.out.println("Error, you choose a Column which does not exist. Please try again.");   // error check so they cant select seat outside of array
        System.out.println("Enter a key to return to Seat Selection Menu");
        aKey = in.next();                                               // enter 'a Key' to move on.
    }

    else {

    if (seats[selectRow][selectColumn] < 0){            
        System.out.println("");                                                                         
        System.out.println("Please choose another seat, this one has already been purchased");

    }
        else {
            System.out.println("");
            System.out.println("Thank you for your purchase of seat Row: "+(selectRow+1) +" Column: " + (selectColumn+1) );   //combine input with the -1 offset 
            System.out.println("");
            System.out.println("");

            seats[selectRow][selectColumn] = -1;                    // flag the seat as taken, any non positives are returned as X in the selection screen.
            }

    System.out.println("Enter a key to return to Seat Selection Menu");
//  aKey = in.next();                                               // enter 'a Key' to move on.





} // column invalid entry check close, 

} //row invalid entry check close

} //theater generator close


}//   ELSE run theater generator close 


}  //main close


          public static boolean theaterFullCheck(int [][] seats, final int rows, final int columns) {





          boolean soldOut = false;

            int i = 0;
        while (i < rows )
        {

            i++;
            int j = 0;
            while( j < columns)
            {

                if (seats[j][i] > 0)   //LINE 180
                {
                    soldOut = false;
                    return soldOut;
                }       

                j++;

            }//for2 close

        } //for1 close     
        soldOut=true;

        return soldOut;






          }


 } //class close

这里是最终输出和崩溃

Enter a key to return to Seat Selection Menu
                                       Row

$10 $10 $10 $10 $10 $10 $10 $10 $10 $10    9

$10  X  $10 $10 $10 $10 $10 $10 $10 $10    8

$10  X  $10 $10 $10 $10 $10 $10 $10 $10    7

$10  X  $20 $20 $20 $20 $20 $20 $10 $10    6

$10  X  $20 $20 $20 $20 $20 $20 $10 $10    5

$10  X  $20 $20 $20 $20 $20 $20 $10 $10    4

$20  X  $30 $30 $40 $40 $30 $30 $20 $20    3

 X   X  $30 $40 $50 $50 $40 $30 $30 $20    2

$30  X  $50 $50 $50 $50 $50 $50 $40 $30    1


 1   2   3   4   5   6   7   8   9   10 
            Column #

  <    S     T     A     G     E    >    

Please input the ROW number of the seat you would like:

9

Please input the COLUMN number of the seat you would like:

2

Thank you for your purchase of seat Row: 9 Column: 2


Enter a key to return to Seat Selection Menu
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at dfg.P6point24.theaterFullCheck(P6point24.java:180)
at dfg.P6point24.main(P6point24.java:61)

0 个答案:

没有答案