我想说如果你输入一个角色,它会做到这一点;否则,如果扫描仪输入== null,请执行此操作。我没有得到任何语法错误,我只需要知道如何写这个,这样如果你没有输入任何字符并点击ENTER然后它将转到我的默认设置。 这是我到目前为止所拥有的。
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());
}
答案 0 :(得分:1)
您可能想要使用input.nextLine()
,然后检查
else if (String.valueOf(characterChoiceSquare).equals("")){
...
}
或者,或者,甚至不进行else-if
检查,如果没有其他else
语句返回true,则会产生最终的if
语句。
另一种方法是使用switch-case
,我建议:
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
}