存储阵列并将其显示在屏幕上

时间:2014-04-01 11:51:58

标签: java

我似乎在向屏幕显示座位号时遇到问题 当我编译它告诉我存储在字符串中的每个数字都是问题

class AirlineSystem{

static int name;

public static void main(String[]args)
            {
String[][] seats = new String[30][6] = {"1a","2a","3a","4a","5a","6a","7a","8a","9a","10a"},
                                        {"11b","12b","13b","14b","15b","16b","17b","18b","19b","20b"},
                                        {"21c","22c","23b","24b","25b","26b","27b","28b","29b","30b"},
                                        {"31d","32d","33d","34d","35d","36d","37d","38d","39d","40d"},
                                        {"41e","42e","43e","44e","45e","46e","47e","48e","49e","50e"},
                                        {"51f","52f","53f","54f","55f","56f","57f","58f","59f","60f"},
                                        {"61g","62g","63g","64g","65g","66g","67g","68g","69g","70g"},
                                        {"71h","72h","73h","74h","75h","76h","77h","78h","79h","80h"},
                                        {"81i","82i","83i","84i","85i","86i","87i","88i","89i","90i"},
                                        {"91j","92j","93j","94j","95j","96j","97j","98j","99j","100j"},
                                        {"101k","102k","103k","104k","105k","106k","107k","108k","109k","110k"},
                                        {"111l","112l","113l","114l","115l","116l","117l","118l","119l","120l"},
                                        {"120m","122m","123m","124m","125m","126m","127m","128m","129m","130m"},
                                        {"131n","132n","133n","134n","135n","136n","137n","138n","139n","140n"},
                                        {"141o","142o","143o","144o","145o","146o","147o","148o","149o","150o"},
                                        {"151p","152p","153p","154p","155p","156p","157p","158p","159p","160p"},
                                        {"161q","162q","163q","164q","165q","166q","167q","168q","169q","170q"},
                                        {"171r","172r","173r","174r","175r","176r","177r","178r","179r","180r"},
                                }

for(i=0;i=seats.length;i++){
System.out.print("Planes seating arrangements"+[seats]);
}

printarray(seats);
}

5 个答案:

答案 0 :(得分:0)

我认为您试图以错误的方式访问数组,在java中尝试访问需要执行此操作的数组元素时

array[index]

因此您需要将for循环更改为:

for(i=0;i=seats.length;i++){
    System.out.print("Planes seating arrangements"+seats[i]);
}

但由于它是一个二维数组,你需要有嵌套循环来遍历你的列/行

希望有所帮助,但在发布堆栈溢出之前请先查看这些内容。

答案 1 :(得分:0)

不像你正在使用String [] []席位。

所以使用嵌套for循环来进行retreival

示例:

  

for(){

     

for(){}}

并且座位[i] [j];

答案 2 :(得分:0)

您的代码有很多错误。试试这种方式:

public static void main(String[] args) {
    String[][] seats = new String[][] {
            { "1a", "2a", "3a", "4a", "5a", "6a", "7a", "8a", "9a", "10a" },
            { "11b", "12b", "13b", "14b", "15b", "16b", "17b", "18b", "19b", "20b" },
            { "21c", "22c", "23b", "24b", "25b", "26b", "27b", "28b", "29b", "30b" },
            { "31d", "32d", "33d", "34d", "35d", "36d", "37d", "38d", "39d", "40d" },
            { "41e", "42e", "43e", "44e", "45e", "46e", "47e", "48e", "49e", "50e" },
            { "51f", "52f", "53f", "54f", "55f", "56f", "57f", "58f", "59f", "60f" },
            { "61g", "62g", "63g", "64g", "65g", "66g", "67g", "68g", "69g", "70g" },
            { "71h", "72h", "73h", "74h", "75h", "76h", "77h", "78h", "79h", "80h" },
            { "81i", "82i", "83i", "84i", "85i", "86i", "87i", "88i", "89i", "90i" },
            { "91j", "92j", "93j", "94j", "95j", "96j", "97j", "98j", "99j", "100j" },
            { "101k", "102k", "103k", "104k", "105k", "106k", "107k", "108k", "109k", "110k" },
            { "111l", "112l", "113l", "114l", "115l", "116l", "117l", "118l", "119l", "120l" },
            { "120m", "122m", "123m", "124m", "125m", "126m", "127m", "128m", "129m", "130m" },
            { "131n", "132n", "133n", "134n", "135n", "136n", "137n", "138n", "139n", "140n" },
            { "141o", "142o", "143o", "144o", "145o", "146o", "147o", "148o", "149o", "150o" },
            { "151p", "152p", "153p", "154p", "155p", "156p", "157p", "158p", "159p", "160p" },
            { "161q", "162q", "163q", "164q", "165q", "166q", "167q", "168q", "169q", "170q" },
            { "171r", "172r", "173r", "174r", "175r", "176r", "177r", "178r", "179r", "180r" } };

    for (int i = 0; i < seats.length; i++) {
        System.out.println("Planes seating arrangements " + Arrays.toString(seats[i]));
    }
}

答案 3 :(得分:0)

试试这个:

String[][] seats = {{"1a","2a","3a","4a","5a","6a","7a","8a","9a","10a"},
                                        {"11b","12b","13b","14b","15b","16b","17b","18b","19b","20b"},
                                        {"21c","22c","23b","24b","25b","26b","27b","28b","29b","30b"},
                                        {"31d","32d","33d","34d","35d","36d","37d","38d","39d","40d"},
                                        {"41e","42e","43e","44e","45e","46e","47e","48e","49e","50e"},
                                        {"51f","52f","53f","54f","55f","56f","57f","58f","59f","60f"},
                                        {"61g","62g","63g","64g","65g","66g","67g","68g","69g","70g"},
                                        {"71h","72h","73h","74h","75h","76h","77h","78h","79h","80h"},
                                        {"81i","82i","83i","84i","85i","86i","87i","88i","89i","90i"},
                                        {"91j","92j","93j","94j","95j","96j","97j","98j","99j","100j"},
                                        {"101k","102k","103k","104k","105k","106k","107k","108k","109k","110k"},
                                        {"111l","112l","113l","114l","115l","116l","117l","118l","119l","120l"},
                                        {"120m","122m","123m","124m","125m","126m","127m","128m","129m","130m"},
                                        {"131n","132n","133n","134n","135n","136n","137n","138n","139n","140n"},
                                        {"141o","142o","143o","144o","145o","146o","147o","148o","149o","150o"},
                                        {"151p","152p","153p","154p","155p","156p","157p","158p","159p","160p"},
                                        {"161q","162q","163q","164q","165q","166q","167q","168q","169q","170q"},
                                        {"171r","172r","173r","174r","175r","176r","177r","178r","179r","180r"}
                                };

for(String[] d1: seats) {
    for (String d2 : d1) {
        System.out.printf("%s\t",d2);
    }
    System.out.println();
}

答案 4 :(得分:0)

我个人认为以下方法是更好的方法:

public class AirlineSystem
{
  public AirlineSystem()
  {
    final String[][] seats = initializeSeats(18, 10);
    displaySeatingArrangement(seats);
  }

  private String[][] initializeSeats(final int ROWS, final int COLUMNS)
  {
    final String[][] seats = new String[ROWS][COLUMNS];
    int seatIndex = 1;

    for (int i = 0, ch = 'a'; i < ROWS; i++, ch++)
    {
      for (int j = 0; j < COLUMNS; j++)
      {
        seats[i][j] = (seatIndex++) + "" + (char) ch;
      }
    }

    return seats;
  }

  private void displaySeatingArrangement(final String[][] seats)
  {
    System.out.println("Plane Seating Arrangement: ");

    for (String[] row : seats)
    {
      for (String seat : row)
      {
        System.out.printf(" %4s ", seat);
      }

      System.out.println();
    }
  }

  public static void main(String[] args)
  {
    new AirlineSystem();
  }
}

它很好,因为它不需要对座位安排进行硬编码。 for循环以非常紧凑的方式生成排列,并且为了清晰起见,输出也被正确格式化。