如何使if语句检查字符串是否大于10个字符
问题描述:
到目前为止,我已经能够找出字符串中有多少个字符了,我似乎无法找出if语句来检查,如果字符有超过10个字符,如果它确实只显示第10如何使if语句检查字符串是否大于10个字符
import java.text.*
public class StringPracticeCoding
{
public static void main (String[] args)
{
//local constants
//local variables
//phrase input by the user
String sentence = "I am somebody, I am worthy";
/******************** Start main method *****************/
System.out.println("Length of String: " + sentence.length());
System.out.println(sentence.substring(0,10));
是的,它必须是一个if语句
答
我不知道这个问题的目的。你已经做了sentence.length()
- 总之,这里的代码来测试,如果它的长度超过10个字符: -
String sentence = "I am somebody, I am worthy";
if (sentence.length() > 10) {
// Do stuff
} else {
// Do different stuff
}
+0
非常感谢,是啊我屠杀了标题了一下,我知道如何显示多达10我只是无法弄清楚如果 – user8723490
答
public class TestProject {
public static void main (String[] args)throws IOException {
String sentence = "I am somebody, I am worthy";
if(sentence.length() > 10){
System.out.println("Greater then 10");
} else {
System.out.println("Less then 10");
}
}
}
如何检查,如果数大于10? – markspace