2011-03-07 73 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; 

        } 
       }