简体   繁体   English

如何在Java中获取“ char”数组的输入?

[英]How to take input for 'char' array in Java?

I am developing a small application to grade Multiple Choice Questions submitted by the user. 我正在开发一个小型应用程序,对用户提交的多项选择题进行评分。 Each question has obviously 4 choices. 每个问题显然有4个选择。 A,B,C,D. A B C D。 Since these answers will be stored in a two dimensional array, I want to ask how can I take input from user for char variable. 由于这些答案将存储在二维数组中,因此我想问一下如何从用户那里获取char变量的输入。 I have not learnt any method to take input for char arrays on console. 我还没有学会在控制台上为char数组输入输入的任何方法。 ie I have just worked with nextInt(), nextDouble(), nextLine() etc. These methods are for Strings and Integers not for char. 即我刚刚使用nextInt(),nextDouble(),nextLine()等。这些方法适用于Strings和Integers而不适用于char。 How to take input for char arrays? 如何获取字符数组的输入? I am going to post code snippet of taking input so that you people can better understand. 我将发布输入代码段,以便您可以更好地理解。

public class MCQChecker{

    public static void main(String []args)
    {
        Scanner input=new Scanner(System.in);
        char[][] students=new char[8][10];

        for (int i=0;i<8;i++)
        {
            System.out.println("Please enter the answer of "+students[i+1]);
            for(int j=0;j<10;j++)
            {
                students[i][j]=?;//Im stuck here
            }
        }
    }
}

一旦获得.next()值作为String ,请检查其.length() == 1 ,然后使用yourString.charAt(0)

   students[i][j]=input.next().charAt(0);

What you need is more than char to handle your requirement. 您需要的不仅仅是char来满足您的需求。 Create a question class which will have question and correct answer, user entered answer. 创建一个具有问题和正确答案的问题类,用户输入答案。

public static class Question {

    private Choice correctChoice = Choice.NONE;
    private Choice userChoice = Choice.NONE;
    private String question = "";

    public Question(String questionString, Choice choice) {
        this.question = questionString;
        this.correctChoice = choice;
    }

    public void setUserChoice(String str) {
        userChoice = Choice.valueOf(str);
    }

    public boolean isQuestionAnswered() {
        return correctChoice == userChoice;
    }

    public String question() {
        return question;
    }

}

enum Choice {
    A, B, C, D, NONE
}

Now you can create a List of questions and for each question you can check whether it was answered correctly or not. 现在,您可以创建一个问题列表,对于每个问题,您都可以检查其答案是否正确。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    List<Question> questions = new ArrayList<Question>();
    questions.add(new Question("question1", Choice.A));
    questions.add(new Question("question2", Choice.A));
    questions.add(new Question("question3", Choice.A));
    for (Question q : questions) {
        System.out.println("Please enter the answer of " + q.question());
        String str = input.next();
        q.setUserChoice(str);
        System.out.println("You have answered question "
                + (q.isQuestionAnswered() == true ? "Correctly"
                        : "Incorrectly"));
    }

}

Above program now allows you to ask questions and reply to user accordingly. 上面的程序现在允许您提出问题并相应地答复用户。 When question is asked if choice entered other than correct answer then question will be marked incorrectly. 当问问题是否输入了正确答案以外的其他选项时,问题将被错误地标记。

In above example if other character is entered than A then it will tell user that you are incorrect. 在上面的示例中,如果输入的字符不是A则它将告诉用户您不正确。

You can't take input directly in charArray as because there is no nextChar() in Java. 您不能直接在charArray中接受输入,因为Java中没有nextChar()。 You first have to take input in String then fetch character one by one. 您首先必须在String中输入内容,然后一个个地获取字符。

import java.util.*;
class CharArray{
    public static void main(String[] args)
    { 
    Scanner scan=new Scanner(System.in); 

    char ch[]=new char[11];

    String s = scan.nextLine();

    for(int i=0;i<=10;i++)  
    ch[i]=s.charAt(i);  //Input in CharArray

    System.out.println("Output of CharArray: ");
        for(int i=0;i<=10;i++) 
        System.out.print(ch[i]); //Output of CharArray
    }
}

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

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