Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描仪输入==空?_Java_If Statement_Null - Fatal编程技术网

Java 扫描仪输入==空?

Java 扫描仪输入==空?,java,if-statement,null,Java,If Statement,Null,我想说的是,如果你输入一个字符,它就会这样做;否则,如果scanner input==null,则改为执行此操作。我没有得到任何语法错误,我只需要知道如何字,这样,如果你不输入任何字符,并点击回车,那么它将进入我的默认设置。 这是我到目前为止所拥有的 Shape sq = new Square(10); System.out.println("Choose character to fill your shape. A-Z, a-z, ! # $ % &

我想说的是,如果你输入一个字符,它就会这样做;否则,如果scanner input==null,则改为执行此操作。我没有得到任何语法错误,我只需要知道如何字,这样,如果你不输入任何字符,并点击回车,那么它将进入我的默认设置。 这是我到目前为止所拥有的

Shape sq = new Square(10);
                System.out.println("Choose character to fill your shape. A-Z, a-z, ! # $ % & ( ) * + Press ENTER.");
                characterChoiceSquare = input.next();
                if(characterChoiceSquare == input.next())
                {
                    for(int m = 0; m < shapeChars.length; m++)
                    }
                    {
                        if(characterChoiceSquare.equals(shapeChars[m]));
                        {
                            char[] c1 = characterChoiceSquare.toCharArray();
                            char[] shapeCharacter = new char[sq.getSizeInt()];
                            for(int i = 0; i < sq.getSizeInt(); i++) 
                            {
                                shapeCharacter[i] = c1[0]; // repeat the char input to fit shapeString
                            }
                            string = String.valueOf(shapeCharacter); //assign the value of the arraylist to shapeString

                            shapeString += string + "\n";

                            System.out.print(shapeString);
                        }
                    }
                }
                else if(characterChoiceSquare == null)
                {
                    System.out.print(sq.displayCharacters());
                }
Shape sq=新正方形(10);
System.out.println(“选择要填充形状的字符。A-Z,A-Z,!#$%&()*+按ENTER键”);
characterChoiceSquare=input.next();
if(characterChoiceSquare==input.next())
{
对于(int m=0;m
您可能想使用
input.nextLine()
,那么您的支票将是

else if (String.valueOf(characterChoiceSquare).equals("")){
    ...
}
或者,甚至不进行
else if
检查,只要有一个最终的
else
语句,如果没有其他
if
语句返回true,该语句就会产生结果

另一种方法是使用
开关盒
,我建议:

switch (String.valueOf(characterChoiceSquare)){
    case "a":
        //do stuff
        break;
    case "b":
        //do stuff
        break;
    case "":
        //do stuff if characterChoiceSquare is empty
        break;
    default:
        //Do this if characterChoiceSquare does not match any cases
}

您可以使用.isEmpty()作为条件,当使用不输入任何内容时,只需点击enter即可。我的代码中没有使用if-else语句。这确实给了我错误。到目前为止,如果没有if-else的想法,它会打印两个选项。如果不是这样的话。但是,我们的想法是要么选择一个字符,要么进入默认值,即随机字符的形状相同。您的意思是如果(scanner.isEmpty()),那么您可以使用@Jonah Haney已经建议过的开关大小写。这是一个内置的scanner,还是我必须创建一个新方法?作为字符串的(characterChoiceSquare.isEmpty())与(characterChoiceChoiceSquare.toString().isEmpty())作为字符或偶数(String.valueOf(characterChoiceSquare.isEmpty))@Steps Oh是
characterChoiceSquare
Character或
String
?@Jonah-它是一个char@StephS我更新了我的答案以反映这一点。至于你最初的问题,不,不是
characterChoiceSquare.isEmpty()
不存在。但是其他的选项都是正确的。好吧,我试过了,但它们不起作用。他们只是什么也不做,因为我有他们。