0

我正在尝试创建一个函数,该函数将从字母表中随机生成字母,用户需要输入“A”作为元音字母和“B”作为辅音。然后系统将通过从数组中搜索字母来检查答案是否正确。

但是,我收到此错误:

找不到标志

符号:方法 indexOf(char)

请在下面检查我的代码。

public static void Exam_LetterType() throws Exception
{
    BufferedReader temp = new BufferedReader(new InputStreamReader(System.in));

    String Alphabet[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    String Vowel[]={"a","e","i","o","u"};
    String Consonant[]={"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};

    Random x = new Random();

    int Index;
    String Answer;

    int CorrectAnswer = 0;

    for(int i=1;i<=20;i++)
    {
        Index = x.nextInt(26);

        System.out.println ("\n         A) VOWEL LETTER        B) CONSONANT LETTER");
        System.out.print("         Letter" + "'" + Alphabet[Index] + "'" + " is a: ");

        String Question = Alphabet[Index];  

        Answer = temp.readLine();

        if(Vowel.indexOf(Question).equals(-1))
            if(Answer.equals("B"))
            {
                CorrectAnswer = CorrectAnswer + 1;
            }
            else if(Consonant.indexOf(Question).equals(-1))
            {
                    if(Answer.equals("A"))
                    {
                        CorrectAnswer = CorrectAnswer + 1;
                    }
                    else
                    {
                        CorrectAnswer = CorrectAnswer + 0;
                    }   
            }   
    }
}

期待你的回复。谢谢!

4

4 回答 4

2

确保数组已排序并使用 Arrays.binarySearch

于 2013-10-03T04:18:35.027 回答
1

首先,Array 类indexOf中没有方法。

甚至在这里

if(Vowel.indexOf(Question).equals(-1))

你不能与equals 你应该使用的整数值进行比较==

那条线应该是

if(// check here that weather the value **i** lesser than the length of array)
    if(Vowel[i].indexOf(Question)== -1))

这样就解决了,由于您要进行多次比较,只需编写一个 Util 方法,该方法检查字符串是否存在array

 public boolean arrayContainsString(String[] arrayToLookUp, String str){
        boolean contains = false;
        for (String item : arrayToLookUp) {
            if (str.equalsIgnoreCase(item)) { //case or not
                contains = true;
                break; // No need to look further.
            } 
        }
        return contains;
    }

然后你可以使用这种方法,简单地

if(arrayContainsString(Vowel, Question)){
   //proceed furthur
}

附带说明:请遵循java 命名约定.varaibles 名称以小写字母开头。

于 2013-10-03T04:16:10.473 回答
0

尝试

if(Arrays.asList(Vowel).contains(Question))
于 2013-10-03T04:17:32.440 回答
0

试试这个..

您需要更改如下所示的两个条件

Vowel[i].indexOf(Question) == -1

Consonant[i].indexOf(Question) == -1

您可以在下面找到完整的编辑代码:

public static void Exam_LetterType() throws Exception
{
    BufferedReader temp = new BufferedReader(new InputStreamReader(System.in));

        String Alphabet[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        String Vowel[]={"a","e","i","o","u"};
        String Consonant[]={"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"};

        Random x = new Random();

        int Index;
        String Answer;

        int CorrectAnswer = 0;

        for(int i=1;i<=20;i++)
        {
            Index = x.nextInt(26);

            System.out.println ("\n         A) VOWEL LETTER        B) CONSONANT LETTER");
            System.out.print("         Letter" + "'" + Alphabet[Index] + "'" + " is a: ");

            String Question = Alphabet[Index];  

            Answer = temp.readLine();

            if(Vowel[i].indexOf(Question) == -1)
                if(Answer.equals("B"))
                {
                    CorrectAnswer = CorrectAnswer + 1;
                }
                else if(Consonant[i].indexOf(Question) == -1)
                {
                        if(Answer.equals("A"))
                        {
                            CorrectAnswer = CorrectAnswer + 1;
                        }
                        else
                        {
                            CorrectAnswer = CorrectAnswer + 0;
                        }   
                }   
        }}
于 2013-10-03T04:19:43.397 回答