简体   繁体   English

如何读取数组中的字符输入?

[英]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.

相关问题 如何将Java字符中的一个字符读取到一个整数数组中? - How do I read a file in Java char by char into an integer array? 如何将文本文件读入2D char数组? - How do I read a text file into a 2D char array? 如何将 int 数组作为单个 3 位输入读取? - how do I read in an int array as a single 3 digit input? 如何过滤用户输入以接受 char 和 int,但不接受 specialchar。 然后只将 int 放入一个数组中 - How do I filter a user input to accept char and int, but not specialchar. Then only takes the int into an array 如何将字符串添加到 char 数组? - How do I add String to a char array? 你如何从文件中的文本中比较 char[] 数组(输入)? - How do you compare char[] array (input) from the text in file? 我如何使用char(从键盘方法System.in.read()输入的字符)停止无限循环。 - how do i stop a infinite loop using char(input from keyboard method-System.in.read( ). System.in? 如何修改此Client类以从字符串数组中读取输入(而不是从标准输入中读取)? - How do I modify this Client class to read input from an array of Strings (rather than from standard input)? 如何读取文本文件的内容并将其复制到 char[][] 数组? - How can I read and copy the contents of a text file to a char[][] array? 如何在命令行中读取用户的char - How do I read a char from a user in the command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM