题
如何用Java编写,显示if
如果变量Goodbye!
包含一个字母的word
声明d
?
<强>由于每个人强>
解决方案
if (word.contains("d")) {
System.out.println("Goodbye!");
}
其他提示
使用:
if(word.indexOf("d") >= 0) {
System.out.println("Goodbye!");
}
查找了Java API文档,看看有什么是String类可用。有几种选择,包括一个返回-1,如果给定的字符不在字符串和字符的索引,如果它在字符串被找到的indexOf()方法。
int ans = mystring.indexOf(mychar);
可以然后使用if语句来检查ANS变量。
if (word.contains("d")) System.out.println("Goodbye!");
嗯,这是在Java中!!
if(word.compareTo("d") == 0)
System.out.println("Goodbye!");
或
if(word.equals("d"))
System.out.println("Goodbye!");
这是假设的当然,这词是字串,而不是一个炭。
不隶属于 StackOverflow