2017-10-05 351 views
-5

到目前爲止,我已經能夠找出字符串中有多少個字符了,我似乎無法找出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語句

+5

如何檢查,如果數大於10? – markspace

回答

0

我不知道這個問題的目的。你已經做了sentence.length() - 總之,這裏的代碼來測試,如果它的長度超過10個字符: -

String sentence = "I am somebody, I am worthy"; 
if (sentence.length() > 10) { 
    // Do stuff 
} else { 
    // Do different stuff 
} 
+0

非常感謝,是啊我屠殺了標題了一下,我知道如何顯示多達10我只是無法弄清楚如果 – user8723490

0
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"); 
     } 
    } 
} 
相關問題