Java输入char到从另一个类访问的数组

时间:2018-07-15 23:33:21

标签: java arrays methods

为了透明起见,这是我正在上课的作业。我不希望别人为我做这件事,我只是在向正确的方向要求一点微调。在此作业中,我需要对用户参加的测试进行评分。给出了答案键。我遇到的问题是如何将char值输入到数组Driver.setAnswerSet中。我已经广泛地搜索了课堂上提供的材料,并且在Eclipse中尝试了许多不同的解决方案来解决我遇到的这个问题。确切地说,我不确定如何将值输入到数组中。我可以将值放入在同一类中初始化的常规数组中,但是调用数组的额外工作在这里已成为我的终结。当从另一个类调用该数组时,如何将用户的输入放入有问题的数组中?我当前遇到的错误是“类型为SU2018LAB6_DriverCandidate_Wayne的方法setAnswerSet(char [])不适用于参数(char)。”同样,我要提供的只是一条建议或在此处朝正确的方向发展。任何帮助或协助将不胜感激。如果我问什么似乎是一个愚蠢的问题,我也真的道歉。

这是我拥有的数据类文件。

public class SU2018LAB6_DriverCandidate_Wayne {

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

    //the answer key to be graded off of
    private char[] answerSet;
    //the answer key that is inputted by the user
    private String lastName;
    private String firstName;
    private String socialNumber;
    private String phone;
    private String address;


    //the getters and setter made by Eclipse
    public char[] getKeySet() {
        return keySet;
    }
    public void setKeySet(char[] keySet) {
        this.keySet = keySet;
    }
    public char[] getAnswerSet() {
        return answerSet;
    }
    public void setAnswerSet(char[] answerSet) {
        this.answerSet = answerSet;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getSocialNumber() {
        return socialNumber;
    }
    public void setSocialNumber(String socialNumber) {
        this.socialNumber = socialNumber;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

项目的驱动程序类:

public static void main(String[] args) {
    SU2018LAB6_DriverCandidate_Wayne Driver = new SU2018LAB6_DriverCandidate_Wayne();
    Scanner keyboard = new Scanner(System.in);
    int test = 1;
    int i;
    int score;

    while (test == 1) {
        System.out.println("Welcome to the Online Driving Test");
        System.out.println("To begin, enter your last name");
        Driver.setLastName(keyboard.nextLine());
        System.out.println("Enter your first name");
        Driver.setFirstName(keyboard.nextLine());
        System.out.println("Enter your SS number");
        Driver.setSocialNumber(keyboard.nextLine());
        System.out.println("Enter your phone number");
        Driver.setPhone(keyboard.nextLine());
        System.out.println("Enter your address");
        Driver.setAddress(keyboard.nextLine());
        //for (i = 0; i < Driver.getKeySet().length; i++) {
        //    System.out.println(Driver.getKeySet()[i]);
        //}
        System.out.println("Driver License Test");
        System.out.println("There are 25 multiple choice questions");
        System.out.println("You have to get at least 20 questions correct to pass");
        System.out.println("---------------------------------");


        // This is the area that I am having trouble with
        for (i = 0; i < Driver.getKeySet().length; i++) {
            System.out.println("Question " + (i + 1) + ": ");
            Driver.setAnswerSet(keyboard.next().charAt(0));
        }
        for (i = 0; i < Driver.getKeySet().length; i++) {
            System.out.println(Driver.getAnswerSet()[i]);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

错误“类型为SU2018LAB6_DriverCandidate_Wayne的setAnswerSet(char [])方法不适用于参数(char)”错误告诉您setAnswerSet方法需要一个字符数组,而不是单个字符。该方法让您用传入的新值覆盖对数组的引用。

您有两个选择:在SU2018LAB6_GradingDLTest_Wayne中创建一个填充有答案的数组,并将其“设置”为新的answerSet,或者在SU2018LAB6_DriverCandidate_Wayne中初始化answerSet并“获取”它,以便您可以开始填写答案。

由于它是当前编写的,因此您不能仅获取和更新answerSet,因为该数组尚未初始化,并且只是空引用。