线性搜索时二维数组索引越界错误



我正在编写一个程序,它应该像一个寻票器。它显示了一个可能的座位选择图表以及它们的价格,并询问用户是想按号码还是按价格选择座位。它的工作原理就像按编号查找座位一样,但当我试图按价格查找座位时,我得到一个数组索引越界错误。我很困惑,因为它应该从0点开始线性搜索。我不明白为什么会有这个错误。

import java.util.Scanner;
public class FindTicket{
  public static void main(String[] args){
    String answer="number";
    Scanner kb=new Scanner(System.in);
    int[][] seats= {
      {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,40,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}
    };
    printChart(seats);
    do{
      System.out.println("Would you like to choose a seat by number, price, or quit?");
      answer = kb.nextLine();
      if(answer.equals("price")){
        sellSeatbyPrice(seats);}
      if(answer.equals("number")){ 
        sellSeatbyNumber(seats);}
      printChart(seats);
    }while(!answer.equals("quit"));
  }
  public static void printChart(int[][] seats){
    for (int i=0; i<seats.length; i++)
    {
      for(int j=0; j<seats[0].length; j++)
      {
        System.out.printf("%8d", seats[i][j]);
      }
      System.out.println();
    }
  }
  public static int[][] sellSeatbyPrice(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int ticketprice;
    int row = 0, col = 0;
    boolean found = false, seatavaliable=true;
    do{
      System.out.println("What is your prefered ticket price?");
      ticketprice=kb.nextInt();
      while (row<seats.length && !found){
        do{
          if(seats[row][col] == ticketprice){
            found = true;}
          else{
            col++; }  
        }while(col<seats[0].length &&!found);
        if(seats[row][col] == ticketprice){
          found = true;}
        else{
          row++;} 
      }
      if(found){
        seats[row][col] = 0; }
      else {
        System.out.println("Seat not found at specified price.");
        seatavaliable=false;}
    }while(seatavaliable==false);
    return seats;
  }
  public static int[][] sellSeatbyNumber(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int row = 0, col = 0;
    int editedrow, editedcol;
    boolean seatavaliable = true;
    do{
      System.out.println("What is your prefered seat number?  Please enter row then column.");
      row=kb.nextInt();
      col=kb.nextInt();
      editedrow = 9-row;
      editedcol = col - 1;
      if(seats[editedrow][editedcol] > 0){
        seats[editedrow][editedcol] = 0;}
      else{
        System.out.println("Seat is not avaliable.");
        seatavaliable=false;}
    }while(seatavaliable==false);
    return seats;
  }
}

这都是因为do...while

当这段代码完成后,col将大于数组的长度。

看注释:

public static int[][] sellSeatbyPrice(int[][] seats){
    Scanner kb=new Scanner(System.in);
    int ticketprice;
    int row = 0, col = 0;
    boolean found = false, seatavaliable=true;
    do{
      System.out.println("What is your prefered ticket price?");
      ticketprice=kb.nextInt();
      while (row<seats.length && !found){
        do{
          if(seats[row][col] == ticketprice){
            found = true;}
          else{
            col++; }  // this line, in the last iteration, will make col=seats[0].length
        }while(col<seats[0].length &&!found);
        if(seats[row][col] == ticketprice){ //col with value greater than it should.
          found = true;}
        else{
          row++;} 
      }
      if(found){
        seats[row][col] = 0; }
      else {
        System.out.println("Seat not found at specified price.");
        seatavaliable=false;}
    }while(seatavaliable==false);
    return seats;
  }

nextInt()方法留下n(结束行)符号,并由nextLine()立即拾取,跳过下一个输入。您需要做的是将nextLine()用于所有内容,并稍后解析它:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

这是迄今为止避免问题的最简单方法——不要混合使用"next"方法。只使用nextLine(),然后再解析int s或单独的单词。


你不能关闭另一个Scanner,因为它会关闭底层的InputStream,因此第一个Scanner不能再从同一个InputStream中读取,你得到一个NoSuchElementException


最后注意:当你使用Scanner时,你应该总是关闭它,以节省系统资源。

相关内容

  • 没有找到相关文章

最新更新