-6

如果用户的输入是肯定的,我将如何做到这一点,然后发送消息你成功地猎杀了一头牛!

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!")
4

3 回答 3

5
if (a.equalsIgnoreCase("yes")) {
    ...
}

(或者equals如果你想"yes"完全匹配)

于 2013-07-17T16:54:37.927 回答
1

真的是你需要的吗:

if ("yes".equals(a)) {
        System.out.println("You successfully killed a cow!")

}

请注意字符串文字与输入的反向检查以避免 NPE。

于 2013-07-17T16:56:02.873 回答
1

使用equals方法。对于字符串,它只是检查字符串是否具有相同的值

if(a.equals("yes")){
     System.out.println("You successfully killed a cow!")
}
else{
  //do whatever
}
于 2013-07-17T16:56:23.520 回答