首页 > 解决方案 > 数组不改变值java

问题描述

您好,如何更改 char 数组中的索引值?我尝试了通常的方式 array[][] = ""; 但是当我打印它时,它仍然显示原始的硬编码数组。

这是主要的

    public static void main(String args[]){
    int seatChoice = 0,seatCol = 0; 
    String seat, type;
    char[][]  airplane = {{'*','*','*','*','*','*'},{'*','*','*','*','*','*'},{'*','*','*','*','*','*'},
                        {'*','*','*','*','*','*'},{'*','*','*','*','*','*'},{'*','*','*','*','*','*'},
                        {'*','*','*','*','*','*'},{'*','*','*','*','*','*'},{'*','*','*','*','*','*'},
                        {'*','*','*','*','*','*'},{'*','*','*','*','*','*'},{'*','*','*','*','*','*'},
                        {'*','*','*','*','*','*'}};
    menu(airplane);
    System.out.print("Please input ticket type: ");
    type = input.nextLine();
    System.out.print("Please input desired seat: ");
    seat = input.nextLine();
    seatChoice =  reservation(seat) - 1;
    if(seat.charAt(1) == 'A')
        seatCol = 0;
    if(seat.charAt(1) == 'B')
        seatCol = 1;
    if(seat.charAt(1) == 'C')
        seatCol = 2;
    if(seat.charAt(1) == 'D')
        seatCol = 3;
    if(seat.charAt(1) == 'E')
        seatCol = 4;
    if(seat.charAt(1) == 'F')
        seatCol = 5;
    airplane[seatChoice][seatCol] = 'X';

    System.out.println(seatChoice);
    System.out.println(seatCol);
    System.out.println(airplane[seatChoice][seatCol]);
    showSeat(airplane);
}

这些是其他方法

   public static int reservation(String seat){
    int seatChoice = 0;
    seatChoice = Character.getNumericValue(seat.charAt(0));
    System.out.println("Your seat has been reserved.");
    return seatChoice;
}
public static void showSeat(char[][] airplane){
    int rows = 13, seats = 6, numRow = 1;
    System.out.println("         A  B  C  D  E  F ");
    System.out.println();
    for (int count = 0; count < rows; count++){
        System.out.print("ROW " + numRow + "    ");
        for (int count2 = 0; count2 < seats; count2++){
            System.out.print(" " + airplane[rows-1][seats-1] + " ");
        }
        numRow++;
        System.out.println();
    }
}

标签: javaarrays

解决方案


rowsseats的价值

System.out.print(" " + airplane[rows-1][seats-1] + " ");

不变,它们总是基于int rows = 13, seats = 6,

尝试使用countcount2


推荐阅读