当我尝试编译它时,我的代码继续显示“需要数组但找到了java.lang.String”

时间:2019-03-30 09:40:59

标签: java arrays string bluej

我试图构建TIC-TAC-TOE游戏,为此我使用了二维字符串数组。我宣布它是全球性的。我必须在某个时间点更新下标。但是出了点问题,我也不知道。有人可以告诉我哪里出了问题。


          String[][] a={{"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," a ","  |","   "," b ","  |","   "," c ","   ",},
              {"___","___","__|","___","___","__|","___","___","___"},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," d ","  |","   "," e ","  |","   "," f ","   ",},
              {"___","___","__|","___","___","__|","___","___","___"},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," g ","  |","   "," h ","  |","   "," i ","   ",},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",}};
           int[] b={1,1,1,1,1,1,1,1,1};

           public void printer(int a,String b)
           {
               String[][] d={{"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," a ","  |","   "," b ","  |","   "," c ","   ",},
              {"___","___","__|","___","___","__|","___","___","___"},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," d ","  |","   "," e ","  |","   "," f ","   ",},
              {"___","___","__|","___","___","__|","___","___","___"},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",},
              {"   "," g ","  |","   "," h ","  |","   "," i ","   ",},
              {"   ","   ","  |","   ","   ","  |","   ","   ","   ",}};
               String c=" X ";
             if(a==1)
          {  c=" X ";
            }
            if(a==2)
            {  c=" O ";}

            switch(b)
            {
                case "a":
                if(b[0]==1)
                { a[1][1]=c;
                 b[0]=(c.compareTo("X")==1)?-1:0;break;}

                case "b":
                if(b[0]==1){
                a[1][4]=c;
                b[1]=(c.compareTo("X")==1)?-1:0;break;}

                case "c":
                if(b[0]==1){
                a[1][7]=c;
                b[2]=(c.compareTo("X")==1)?-1:0;break;}

                case "d":
                if(b[0]==1){
                a[4][1]=c;
                b[3]=(c.compareTo("X")==1)?-1:0;break;
            }
                case "e":
                if(b[0]==1)
                {a[4][4]=c;
                b[4]=(c.compareTo("X")==1)?-1:0;break;
            }
                case "f":
                if(b[0]==1){
                a[4][7]=c;
                b[5]=(c.compareTo("X")==1)?-1:0;break;
            }
                case "g":
                if(b[0]==1){
                a[7][1]=c;
                b[6]=(c.compareTo("X")==1)?-1:0;break;
            }
                case "h":
                if(b[0]==1){
                a[7][4]=c;
                b[7]=(c.compareTo("X")==1)?-1:0;
            break;}
                case "i":
                if(b[0]==1){
                a[7][7]=c;
                b[8]=(c.compareTo("X")==1)?-1:0;break;
            }

编译器始终显示错误array expected,but java.lang.String found。我提供了更大的代码,所以有人可以调查此事吗?

在Java中是否允许使用字符串更新字符串数组的下标? 我在switch-case语句中使用了字符串是否出错?

(我对面向对象的编程了解不多。我是该领域的新手。)

如果是String数组

a[7][7]=some string value 错误是array required but integer found

,但如果是整数数组  b[4]=some integer value

错误是array required but string found

我没收到。

1 个答案:

答案 0 :(得分:0)

问题是您在函数外部定义了“ int [] b ”。以及 printer 方法的字符串b

因此在打印机方法中, b 是一个字符串,但是您尝试将其分配为开关盒内的数组。

只需更改以下内容,就可以了。

更改1:public void printer(int a,**String str**)
更改2:switch(**str**)