如何在 Java 中获取“char”数组的输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12870713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take input for 'char' array in Java?
提问by Hammad
I am developing a small application to grade Multiple Choice Questions submitted by the user. Each question has obviously 4 choices. 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. I have not learnt any method to take input for char arrays on console. i.e I have just worked with nextInt(), nextDouble(), nextLine() etc. These methods are for Strings and Integers not for 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.
我正在开发一个小应用程序来对用户提交的多项选择题进行评分。每个问题显然有4个选择。A B C D。由于这些答案将存储在二维数组中,我想问一下如何从用户获取 char 变量的输入。我还没有学习过任何在控制台上输入字符数组的方法。即我刚刚使用过 nextInt()、nextDouble()、nextLine() 等。这些方法用于字符串和整数,而不是用于字符。如何获取字符数组的输入?我将发布接收输入的代码片段,以便大家更好地理解。
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
}
}
}
}
回答by Eric
Once you get the .next()
value as a String
, check if its .length() == 1
, then use yourString.charAt(0)
.
获得.next()
作为 a的值后String
,检查它是否为.length() == 1
,然后使用yourString.charAt(0)
.
回答by Yogendra Singh
students[i][j]=input.next().charAt(0);
回答by Amit Deshpande
What you need is more than char
to handle your requirement. Create a question class which will have question and correct answer, user entered answer.
您需要的不仅仅是char
处理您的要求。创建一个有问题和正确答案的问题类,用户输入的答案。
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
那么它会告诉用户你是不正确的。
回答by Ritu Raj Shrivastava
You can't take input directly in charArray as because there is no nextChar() in Java. You first have to take input in String then fetch character one by one.
您不能直接在 charArray 中接受输入,因为 Java 中没有 nextChar() 。您首先必须在 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
}
}