帮助切换案例

时间:2011-03-07 19:30:57

标签: java

这是我的程序,它将计算所有空格,所有字母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 ();
    }

}

4 个答案:

答案 0 :(得分:3)

你写

switch(ch)

但变量ch永远不会赋值。

答案 1 :(得分: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 ();
    }

}

答案 2 :(得分:0)

有两个问题:首先,当前字符从未分配给ch,第二个switch语句不在for循环中。

答案 3 :(得分:0)

更改Ur这样的语句,开关必须包含在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;

                    }
                }