2014-11-23 40 views
-1

我得到它的工作,但它也正在計數正在鍵入的單詞之間的空格。 我所關心的只是字母數,而不是空格。 我該怎麼做才能改變這種狀況?我在哪裏搞亂或錯過了什麼文字中出現多少字母?

import java.util.Scanner; 

public class LetterCount 
{ 
public static void main (String[] args) 
{ 

    System.out.println("Type something "); 
    Scanner n = new Scanner (System.in); 
    String s = ""; 
    s = n.nextLine(); 
    char []c = s.toCharArray(); 
    int sz = c.length; 
    int i =0, j=0, counter=0; 

    for (i=0; i<sz; i++){ 
     counter =0; 

      for(j=0; j<sz; j++) 
      { 

       if (j< i&& c[i] == c[j]) 
       { 
        break; 
       } 

       if (c[j] == c[i]) 
       { 
        counter++; 
       } 

       if (j==sz-1) 
       { 
        System.out.println("the character "+c[i]+" is present "+counter+" times"); 
       } 
      } 
    } 


} 

}

+1

請在嘗試之前嘗試一下並告訴我們。 – 2014-11-23 19:49:02

+0

啊,是的,功課。 – iamkrillin 2014-11-23 20:00:27

+0

import java.util.Scanner; 公共類LetterCount { \t公共靜態無效的主要(字串[] args) \t { \t \t \t \t的System.out.println( 「輸入內容」); \t \t掃描儀n =新掃描儀(System.in); \t \t String s =「」; \t \t s = n.nextLane(); //告訴找不到符號 \t \t char [] c = s.toCharArray(); \t \t int sz = c.length; \t \t int i = 0,j = 0,counter = 0; \t \t \t 爲\t(I = 0; I Noobie 2014-11-23 20:30:40

回答

相關問題