java 如何创建包含数字和字符数组表

ifmq2ha2  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(129)

实际上,我希望表格的顶部有标题,如no.room、医生姓名和时间。至于时间,我希望它显示为8-10 p.m.,如01,没有房间,医生姓名,如Dr. Ali.,但我不知道如何显示,因为我知道char只能有一位数。

char[][] doctor = new char[5][3];
    
    
        doctor[0][0] = '1';
        doctor[0][1] = 'A';
        doctor[0][2] = '8';
        
        
        doctor[1][0] = '2';
        doctor[1][1] = 'S';
        doctor[1][2] = '1';
    
        doctor[2][0] = '3';
        doctor[2][1] = 'N';
        doctor[2][2] = '3';
        
        doctor[3][0] = '4';
        doctor[3][1] = 'R';
        doctor[3][2] = '1';
        
        doctor[4][0] = '4';
        doctor[4][1] = 'M';
        doctor[4][2] = '3';
    
    

  
    for (int i=0; i < 5 ; i++) {
          for (int j=0; j < 3 ; j++) {
               System.out.print( doctor[ i ][ j ] + " " );
    }

        System.out.println( "" );
}
    
    }
ckx4rj1h

ckx4rj1h1#

不能在char类型中存储一个以上的字符。可以使用String来存储多个字符,而不是使用char类型。这是documentation,供进一步阅读。
下面是使用string的代码。

class HelloWorld {
    public static void main(String[] args) {
        
        String [][] doctor = new String[5][3];
        
        doctor[0][0] = "Room 1:";
        doctor[0][1] = "Dr. Alex";
        doctor[0][2] = "8-10 AM";
        
        doctor[1][0] = "Room 2:";
        doctor[1][1] = "Dr. Mina";
        doctor[1][2] = "10-12 AM";
    
        doctor[2][0] = "Room 3:";
        doctor[2][1] = "Dr. Nadir";
        doctor[2][2] = "3-4 PM";
        
        doctor[3][0] = "Room 4:";
        doctor[3][1] = "Dr. Rahul";
        doctor[3][2] = "1-4 PM";
        
        doctor[4][0] = "Room 5:";
        doctor[4][1] = "Dr. Mahir";
        doctor[4][2] = "3-6 PM";
    
        for (int i=0; i < 5 ; i++) {
            for (int j=0; j < 3 ; j++) {
                System.out.print( doctor[ i ][ j ] + " " );
            }
            System.out.println( "" );
        }
    }
}

相关问题