比较java中数组的char值

时间:2016-10-27 00:37:58

标签: java arrays char

好的,所以我的程序由两个类组成,DriverMain& DriverExam。在DriverExam类中,创建一个char数组并填充答案(正确),然后在主要用户将输入他们的答案,并将其存储到名为correctAns的char数组中。我无法将每个数组中任何字符的值相互比较,以查看用户的答案是否正确。

这是DriverMain类:

import javax.swing.JOptionPane;

public class DriverMain {

public static void main(String[] args) {

    // Create studetnAns array and studentOne
    char[] studentAns = new char[5];
    DriverExam studentOne = new DriverExam();

    // While loop will keep question running until
    // the user has filled up the array
    while(studentAns.length < 4) {

        for (int i = 0; i < studentAns.length; i++) {

            // Load array with user's answers
            char ans = JOptionPane.showInputDialog(null, "Please enter an answer").charAt(0);
            studentAns[i] = ans;
        }
    }

    // Testing getTotalCorrect method
    JOptionPane.showMessageDialog(null, "Your total answers correct is " + studentOne.getTotalCorrect(studentAns));
}

}

这是DriverExam类:

public class DriverExam {

// Instance variables
int correct;
int incorrect;
char [] correctAns;

/**
 * 
 */
public DriverExam() {

    char[] correctAns = {'A', 'B', 'D', 'A', 'C'};

    correct = 0;
    incorrect = 0;
}

public int getTotalCorrect(char[] studentAns) {

    for (int i = 0; i < studentAns.length; i++) {

        if (studentAns[i] == (correctAns[i])) {

            correct += 1;
        }
    }

    return correct;
}

public boolean passed() {

    boolean pass = false;

    return pass;
}

}

0 个答案:

没有答案