【问题标题】:Array required but Integer found even when I'm not returning an array需要数组,但即使我没有返回数组也能找到整数
【发布时间】:2015-11-12 03:36:41
【问题描述】:

我正在编写一个课程,其中包含发送问题的方法,该问题从我计算机中存储的 txt 文件中检索到。
我的编译器在 's=op[qno][op];' 行不断抛出错误(从底部开始的第四行)说“需要数组但找到了 int”。我什至没有尝试返回数组,为什么我会看到这个?我该如何纠正? 打包计算机项目;

import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class Questions
{
    String q[]=new String[30];
    String op[][]=new String[55][5];
    int ord[][]=new int[20][5];

     Questions()throws FileNotFoundException,IOException
     {
         initArray();
         setOrder();
     }

     void initArray()throws FileNotFoundException,IOException
     {
        BufferedReader br=new BufferedReader(new                FileReader("C:\\Users\\mansoor\\Desktop\\Quiz\\Qs.txt"));
        for(int i=0;i<55;i++)
        {
            q[i]=br.readLine();
            op[i][0]=br.readLine();
            System.out.print(op[i][0]);
            op[i][1]=br.readLine();
            op[i][2]=br.readLine();
            op[i][3]=br.readLine();
            op[i][4]=br.readLine();
        }
    }

    void setOrder()
    {
        Random r=new Random();
        for(int i=r.nextInt(55),j=0;j<40;j++,i++)
        {
            ord[j][0]=i%55;
        }
        for(int i=44;i!=0;i--)
        {
            int a=r.nextInt(40);
            int b=r.nextInt(40);
            int t=ord[a][0];
            ord[a][0]=ord[b][0];
            ord[b][0]=t;
            for(int s[]=setRandomOrder(),j=0;j<4;j++)
            {
                ord[i][j]=s[j];
            }
        }
    }

    int[] setRandomOrder()
    {
        Random r=new Random();
        int ar[]={0,1,2,3};
        for(int i=0;i<16;i++)
        {
            int a=r.nextInt(4),b=r.nextInt(4),t;
            t=ar[a];
            ar[a]=ar[b];
            ar[b]=t;
        }
        return ar;
    }

    String sendQuestion(int qno)
    {
        return q[qno];
    }

    String sendCorrectAnswer(int qno)
    {
        return op[qno][4];
    }

    int[] randomOrder()
    {
        Random r=new Random();
        int ar[]=new int[4];
        for(int i=0;i<16;i++)
        {
            int a=r.nextInt(4);
            int b=r.nextInt(4);
            int t=ar[a];
            ar[a]=ar[b];
            ar[b]=t;
        }
        return ar;
    }

    String[] sendOption(int qno,int op)
    {
        String s[]=new String[4];
        for(int i=0;i<4;i++)
        {
            s[i]=op[qno][i];
        }
        return op[qno];
    }
}

【问题讨论】:

  • 你有一个名为op的参数; String[] sendOption(int qno,int op)

标签: java arrays compiler-errors


【解决方案1】:

您没有将数组传递给您的 sendOption 方法。您传入一个整数 ( op ) ,然后在将 s[i] 值放入 (op[qno][i]) 时将其视为先前声明的二维数组。错误可能在这里。我会更改您的第二个输入参数的名称。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    查看您的方法签名sendOption(int qno,int op)。值op 的类型为int。这不是一个数组,这意味着你不能做像op[qno][i]这样的事情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      相关资源
      最近更新 更多