這是我的程序,它會計算所有空格,所有字母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();
}
}
你能告訴我們出現的錯誤嗎? – 2011-03-07 19:32:44
Count.java:42:變量CH可能尚未初始化 \t開關(CH){ \t^ 1錯誤 – 2011-03-07 19:34:20
看代碼,我可以說,'焦ch'未初始化 – asgs 2011-03-07 19:34:44