2011-03-07 136 views
1

這是我的程序,它會計算所有空格,所有字母a,e,s,t大寫或小寫,但編譯時出錯,它會給我一個我的變量未被初始化。幫助開關盒

可能有人來看看,並告訴我

import java.util.Scanner; 

public class Count 
{ 
    public static void main (String[] args) 
    { 
     String phrase; // a string of characters 
     int countBlank; // the number of blanks (spaces) in the phrase 
     int length;  // the length of the phrase 
     char ch;   // an individual character in the string 
     int countA=0,countE=0,countS=0,countT=0; 

    Scanner scan = new Scanner(System.in); 
     // Print a program header 
     System.out.println(); 
     System.out.println ("Character Counter"); 
     System.out.println(); 

     // Read in a string and find its length 
     System.out.print ("Enter a sentence or phrase: "); 
     phrase = scan.nextLine(); 
     length = phrase.length(); 


     // Initialize counts 
     countBlank = 0; 

     // a for loop to go through the string character by character 

     for (int i = 0; i < phrase.length(); i++) 
     { 
    if(phrase.charAt(i) == ' ') countBlank++; 
     } 
    switch(ch) { 
    case 'a': 
    case 'A': countA++; 
    break; 

    case 'e': 
    case 'E': countE++; 
    break; 

    case 's': 
    case 'S': countS++; 
    break; 

    case 't': 
    case 'T': countT++; 
    break; 

    } 

     // Print the results 
     System.out.println(); 
     System.out.println ("Number of blank spaces: " + countBlank); 
     System.out.println ("Number of a: " + countA); 
     System.out.println ("Number of e: " + countE); 
     System.out.println ("Number of s: " + countS); 
     System.out.println ("Number of t: " + countT); 
     System.out.println(); 
    } 

} 
+1

你能告訴我們出現的錯誤嗎? – 2011-03-07 19:32:44

+0

Count.java:42:變量CH可能尚未初始化 \t開關(CH){ \t^ 1錯誤 – 2011-03-07 19:34:20

+0

看代碼,我可以說,'焦ch'未初始化 – asgs 2011-03-07 19:34:44

回答

0

這裏有兩個問題:一是當前字符是永遠不會分配給CH和第二開關語句是不是裏面你的for循環。

+0

啊我看,如果我嵌套在循環下的開關,它的工作原理,然後我可以使用ch = phrase.charAt(i);非常感謝 – 2011-03-07 19:39:17

2

你有「開關(CH)」,但你從來沒有賦予它的價值。你用「char ch」來聲明它但這還不夠。

也許你想做的事:你的循環的

ch = phrase.charAt(i); 

內。

所以你的組合代碼可以是這樣的:

import java.util.Scanner; 

public class Count 
{ 
    public static void main (String[] args) 
    { 
     String phrase; // a string of characters 
     int countBlank; // the number of blanks (spaces) in the phrase 
     int length;  // the length of the phrase 
     char ch;   // an individual character in the string 
     int countA=0,countE=0,countS=0,countT=0; 

    Scanner scan = new Scanner(System.in); 
     // Print a program header 
     System.out.println(); 
     System.out.println ("Character Counter"); 
     System.out.println(); 

     // Read in a string and find its length 
     System.out.print ("Enter a sentence or phrase: "); 
     phrase = scan.nextLine(); 
     length = phrase.length(); 


     // Initialize counts 
     countBlank = 0; 

     // a for loop to go through the string character by character 

     for (int i = 0; i < phrase.length(); i++) 
     { 
      if(phrase.charAt(i) == ' ') countBlank++; 
      ch = phrase.charAt(i); 

       switch(ch) { 
        case 'a': 
        case 'A': countA++; 
        break; 

        case 'e': 
        case 'E': countE++; 
        break; 

        case 's': 
        case 'S': countS++; 
        break; 

        case 't': 
        case 'T': countT++; 
        break; 
       } 
     } 


     // Print the results 
     System.out.println(); 
     System.out.println ("Number of blank spaces: " + countBlank); 
     System.out.println ("Number of a: " + countA); 
     System.out.println ("Number of e: " + countE); 
     System.out.println ("Number of s: " + countS); 
     System.out.println ("Number of t: " + countT); 
     System.out.println(); 
    } 

} 
+0

我應該做ch = phrase.charAt(i);在轉換開始之前或之前? – 2011-03-07 19:35:58

+0

其實如果我做ch = phrase.charAt(i);那麼我得到一個錯誤,我沒有找到 – 2011-03-07 19:36:59

+0

你有它在循環內嗎?我把它全部移到循環中,並且它工作。 – 2011-03-07 19:40:56

3

你寫

switch(ch) 

但是變量ch從未賦值。

0

更改Ur for語句這樣,開關必須包含在for中,並且ch應該被初始化爲第i個字符。

for (int i = 0; i < phrase.length(); i++) 
       { 
       if(phrase.charAt(i) == ' ') 
        { 
         countBlank++; 
        } 
       ch = phrase.charAt(i); 

        switch(ch) 
        { 
         case 'a': 
         case 'A': countA++; 
         break; 

         case 'e': 
         case 'E': countE++; 
         break; 

         case 's': 
         case 'S': countS++; 
         break; 

         case 't': 
         case 'T': countT++; 
         break; 

        } 
       }