简体   繁体   English

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

[英]Java input char to array that is accessed from another class

Just to be transparent, this is an assignment for a class I am taking. 只是为了透明起见,这是我正在上课的一项作业。 I don't want someone else to do this for me, I'm just asking for a little nudge in the right direction. 我不希望别人为我做这件事,我只是在向正确的方向要求一点微调。 In this assignment, I need to grade a test that the user takes. 在此作业中,我需要对用户参加的测试进行评分。 The answer key is given. 给出了答案键。 The thing that I am stuck on is how to input the char values into the array Driver.setAnswerSet. 我遇到的问题是如何将char值输入到数组Driver.setAnswerSet中。 I've extensively searched the material given in the class, and I have tried many different possible solutions in Eclipse to solve this problem I'm having. 我已经广泛地搜索了课堂上提供的材料,并且在Eclipse中尝试了许多不同的解决方案来解决我遇到的这个问题。 To be exact, I'm not sure exactly how to input the values into the array. 确切地说,我不确定如何将值输入到数组中。 Putting the values into a regular array that is initialized in the same class is something I can do, but the addition of calling the array is becoming the end of me here. 我可以将值放入在同一类中初始化的常规数组中,但是调用数组的额外工作在这里已成为我的终结。 How would I go about placing input from the user into the array in question when it's being called from the other class? 当从另一个类调用该数组时,如何将用户的输入放入有问题的数组中? The current error I have is "The method setAnswerSet(char[]) in the type SU2018LAB6_DriverCandidate_Wayne is not applicable for the arguments (char)." 我当前遇到的错误是“类型为SU2018LAB6_DriverCandidate_Wayne的setAnswerSet(char [])方法不适用于参数(char)。” Again, all I'm asking for is a piece of advice or push in the right direction here. 同样,我要提供的只是一条建议或在此处朝正确的方向发展。 Any help or assistance would be greatly appreciated. 任何帮助或协助将不胜感激。 I also really do apologize if I'm asking what seems to be a stupid question. 如果我问什么似乎是一个愚蠢的问题,我也真的道歉。

This is the data class file that I have. 这是我拥有的数据类文件。

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;
    }
}

The driver class of the project: 项目的驱动程序类:

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]);
        }
    }
}

The error "The method setAnswerSet(char[]) in the type SU2018LAB6_DriverCandidate_Wayne is not applicable for the arguments (char)" is telling you that the setAnswerSet method is expecting an Array of characters, not a single character. 错误“类型为SU2018LAB6_DriverCandidate_Wayne的setAnswerSet(char [])方法不适用于参数(char)”错误告诉您setAnswerSet方法需要字符数组,而不是单个字符。 The method let's you overwrite the reference to the array with a new one you pass in. 该方法让您用传入的新数组覆盖对数组的引用。

You have two options: Create an array filled with answers in SU2018LAB6_GradingDLTest_Wayne and "set" it as the new answerSet, or initialize answerSet in SU2018LAB6_DriverCandidate_Wayne and "get" it so you can start filling in the answers. 您有两个选择:在SU2018LAB6_GradingDLTest_Wayne中创建一个填充有答案的数组,然后将其“设置”为新的answerSet,或者在SU2018LAB6_DriverCandidate_Wayne中初始化answerSet并“获取”它,以便您可以开始填写答案。

As it's currently written you can't just get the answerSet and update it because the array hasn't been initialized and is just a null reference. 正如当前所写的那样,您不能仅获取和更新answerSet,因为该数组尚未初始化,只是一个空引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM