[英]How do I read a char input in an array?
I am writing a program that consists of designing a multiple choice test grader.我正在编写一个程序,其中包括设计一个多项选择测试分级机。 I declared an array to store the responses.
我声明了一个数组来存储响应。 The issue I'm having is that, I don't know how to read each user input (char) to see whether it's correct or not.
我遇到的问题是,我不知道如何读取每个用户输入(字符)以查看它是否正确。 The code I have, is just a placeholder.
我拥有的代码只是一个占位符。 I was unable to figure out what to do.
我不知道该怎么做。 This is a snippet of my code:
这是我的代码片段:
for (int a = 0 ; a < response.length ; a++)
{
System.out.print((a + 1) + ": ");
Scanner s = new Scanner(System.in);
response [a] = System.in.read();
while (response [a] < 'A' || response [a] > 'D')
{
System.out.println ("Invalid input. Enter answers with A, B, C, or D only!");
response [a] = System.in.read();
}
}
I am not entirely sure of what you are trying to achieve so I am going to make a few assumptions for my answer:我不完全确定您要实现的目标,因此我将对我的回答做出一些假设:
The user hits the enter key after each answer which seems natural to me if this is a multiple choice test.用户在每个答案后按回车键,如果这是一项多项选择测试,这对我来说似乎很自然。
You will know the correct answer and simply need to check that the input is correct.您将知道正确答案,只需检查输入是否正确即可。
char [] correctAnswers = {'A', 'B', 'C', 'A'};
Scanner in = new Scanner(System.in);
for(int i = 0; i < correctAnswers; i ++){
System.out.println("Please input answer:");
String valueString = in.nextLine().toUpperCase();
if(valueString.length() != 1){
System.out.println("Answer invalid. Please only type one character");
i--;
} else {
char answer = valueString.charAt(0);
if(correctAnswers[i] == answer)
System.out.println("Correct");
else
System.out.println("Wrong");
}
}
This sample should do the trick.这个示例应该可以解决问题。
Alternative选择
If the user is going to enter all of his answers and then hit enter you can access each character as follows:如果用户要输入他的所有答案,然后按 Enter,您可以按如下方式访问每个字符:
char [] correctAnswers = {'A', 'B', 'C', 'A'};
Scanner in = new Scanner(System.in);
System.out.println("Please input " + correctAnswers.length + " answer(s):");
String answerString = in.nextLine().toUpperCase();
if(answerString.length() != correctAnswers.length){
System.out.println("Incorrect number of answers entered");
} else {
for(int i = 0; i < answerString.length; i ++){
if(correctAnswers[i] == answerString.charAt(i)){
System.out.println("Answer " + (i + 1) + " Correct");
} else {
System.out.println("Answer " + (i + 1) + " Wrong");
}
count ++;
}
}
I am not clear about how you are planning to read the input but if you want user to enter the input for each question's answer you can modify the code as below我不清楚您打算如何阅读输入,但如果您希望用户为每个问题的答案输入输入,您可以修改代码如下
for (int a = 0 ; a < response.length ; a++)
{
System.out.print((a + 1) + ": ");
Scanner s = new Scanner(System.in);
response [a] = s.next();
while (response [a] < 'A' || response [a] > 'D')
{
System.out.println ("Invalid input. Enter answers with A, B, C, or D only!");
response [a] = s.next().charAt(0);
}
}
s.next() takes input as a string so you may need to take the first char out as you are trying to do in the loop. s.next() 将输入作为字符串,因此您可能需要在循环中尝试将第一个字符取出。
If you are planning to read input from some file than the reading process would be different, you can look up for that.如果您打算从某个文件读取输入而不是读取过程会有所不同,您可以查找它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.