如何在Java中获取'char'数组的输入? [英] How to take input for 'char' array in Java?

查看:185
本文介绍了如何在Java中获取'char'数组的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小型应用程序来评估用户提交的多项选择题。每个问题显然有4种选择。 A B C D。由于这些答案将存储在二维数组中,我想问一下如何从用户获取char变量的输入。我还没有学习任何方法来在控制台上获取char数组的输入。即我刚刚使用了nextInt(),nextDouble(),nextLine()等。这些方法适用于字符串和整数,不适用于char。如何获取char数组的输入?我将发布输入的代码片段,以便人们可以更好地理解。

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.

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)

Once you get the .next() value as a String, check if its .length() == 1, then use yourString.charAt(0).

这篇关于如何在Java中获取'char'数组的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆