错误]预期方法

时间:2018-05-27 21:18:53

标签: java

我不确定为什么我无法将数组设置为j或g进行比较?有更聪明的方法吗?

在make gradeExam和makeMissedArray方法之前我没有问题。 if语句会出现这些错误

DriverTest.java:72: error: ']' expected
                        if (char[j] answersUppercase != (char[j] answerKey){
                                 ^
DriverTest.java:72: error: ')' expected
                        if (char[j] answersUppercase != (char[j] answerKey){
                                  ^
DriverTest.java:72: error: ']' expected
                        if (char[j] answersUppercase != (char[j] answerKey){
                                                              ^
DriverTest.java:72: error: ')' expected
                        if (char[j] answersUppercase != (char[j] answerKey){
                                                               ^
DriverTest.java:72: error: not a statement
                        if (char[j] answersUppercase != (char[j] answerKey){
                                                     ^
DriverTest.java:72: error: ';' expected
                        if (char[j] answersUppercase != (char[j] answerKey){
                                                                ^
DriverTest.java:72: error: illegal start of expression
                        if (char[j] answersUppercase != (char[j] answerKey){
                                                                          ^

代码:

public static void main (String [] args){

Scanner input = new Scanner(System.in);
String passedExam = null;
char[] studentAnswers = new char[20];
char[] answerKey = {'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'D'}; 


    // make the string for the answers
    collectStudentAnswers(studentAnswers);

    //change the answers to uppercase
    answersUppercase(studentAnswers);

    //comapre the two strings with i=0 to 20
    gradeExam(answerKey, studentAnswers);
    makeMissedArray(answerKey, studentAnswers);

    passedExam(gradeExam);

    System.out.printf("** Correct answers: %f", 20 - gradeExam);
    System.out.printf("** Incorrect answers: %f", gradeExam);
    System.out.printf("** %s", passedExam);
    System.out.printf("You missed the following questions:\n ");
    System.out.println(Array.toString(makeMissedArray));


    } public static void collectStudentAnswers(char[] studentAnswers){

        for (int i = 0; i < 20;){

    System.out.printf("Question %d:\n ", i + 1);
    char[] studentAnswers = input.next().charAt(0);

    i++;

    } 

    return studentAnswers;

    } public static char[] answersUppercase(char[] studentAnswers){

        studentAnswers.toUpperCase();
        return answersUppercase;


    } public static String passedExam (int gradeExam) {

        if (20 - gradeExam >= 15){

            passedExam = "You passed the exam.";

        } else {

            passedExam = "You failed the exam!";

        }

        return passedExam;


    } public static int gradeExam(char[] answerKey, char[] answersUppercase){

        for (int j = 0; j < 20;){


        if (char[j] answersUppercase != (char[j] answerKey){

            gradeExam++;    

            }   

        }

        return gradeExam;
        j++;        

    } public static int[] makeMissedArray(char[] answerKey, char[] answersUppercase){

        for (int g = 0; g < 20;){

            if (char[g] answersUppercase != (char[g] answerKey){

                makemissedArray[j] = g;


            }

            g++;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

这只是不正确的语法:

if (char[j] answersUppercase != (char[j] answerKey){

除了不匹配的括号外,这不是您访问数组元素的方式。

也许你的意思是:

if (answersUppercase[j] != answerKey[j]){ 

但即使在解决了这个问题之后,你的代码中还有其他一些问题(以及一些奇怪的问题并没有错误&#34;只是不是惯用的)。

我建议您从阅读Java教程中受益,特别是关于数组和for循环。

答案 1 :(得分:0)

你应该像这样编写你的

if (answersUppercase[g] != answerKey[g]){.....}

在你的方法中