如果用户的输入是肯定的,我将如何做到这一点,然后发送消息你成功地猎杀了一头牛!
System.out.println("Would you like to hunt a cow?");
System.out.print("yes or no?");
String a = kbReader.next();
System.out.println("You successfully hunted a cow!")
如果用户的输入是肯定的,我将如何做到这一点,然后发送消息你成功地猎杀了一头牛!
System.out.println("Would you like to hunt a cow?");
System.out.print("yes or no?");
String a = kbReader.next();
System.out.println("You successfully hunted a cow!")
if (a.equalsIgnoreCase("yes")) {
...
}
(或者equals
如果你想"yes"
完全匹配)
真的是你需要的吗:
if ("yes".equals(a)) {
System.out.println("You successfully killed a cow!")
}
请注意字符串文字与输入的反向检查以避免 NPE。
使用equals方法。对于字符串,它只是检查字符串是否具有相同的值
if(a.equals("yes")){
System.out.println("You successfully killed a cow!")
}
else{
//do whatever
}