2017-04-04 94 views
-1

我知道我錯過了線,我相信它的sudo的是:對於i = 0至str.length - 1:查找最長集合在一個字符串連續字符使用Java

我把那哪裏我認爲它應該去,但我不知道我會怎麼寫它?對此很新,任何幫助表示讚賞!

public static void main (String[] args) 
{ 
    c = new Console(); 

    String size; 

    c.print ("Enter a string: "); 
    size = c.readString(); 

    String answer = sizeOfLargestGroup (size); 

    c.println (answer); 
} // main method 


private static String sizeOfLargestGroup (String size) 

{ 
    maxCount = 1; 
    currCount = 1; 
    for i = 0 to str.length - 1: 
    if (currCount > maxCount) 
     maxCount = currCount; 
    if (str [i] == str [i + 1]) 
     currCount++; 
    else 
     currCount = 1; 
    return maxCount; 

} 
+1

搜索谷歌*的Java for循環* - 絕對不會你使用的語法是 –

+0

你的意思是使用循環而不是方法? – SoloTriesToLearn

+1

你知道'for'的用途嗎?它循環 –

回答

0

考慮我下面的回答,我在方法聲明2 INT,一個知道有多少個字母和一個知道這是更大的

public static void main(String[] args) 
      { 
       Scanner scan = new Scanner(System.in); 
       System.out.println("Please enter a string"); 
       String mySize=scan.nextLine(); 
       int answer=sizeOfLargestGroup(mySize); 
       System.out.println(answer); 
      } 
      public static int sizeOfLargestGroup (String size) 
      { 
       int myCharInt=1;//get the character 
       int myCurrent=0;//get the largest myCharInt 
       for(int i=0;i<size.length()-1;++i) 
       { 

        if(size.charAt(i)==size.charAt(i+1)) 
         myCharInt++; 

        else 
         myCharInt=1; 
        if(myCurrent<myCharInt)//test the current value of myCharInt if it is largest switch it to the current value of myCurrent 
        myCurrent=myCharInt; 

       } 

       return myCurrent; 


      } 
相關問題