使用switch case将字符串转换为char

时间:2013-10-24 09:28:36

标签: java

我正在从字符串中获取用户的输入,我想使用case语句进行迭代和测试,但它无法正常工作。它不打印报表。

import java.io.*;
import java.util.*;

public class fh3

{
    public static void main(String args[])throws IOException

{

    String sentence = "";

    System.out.println("Enter the word : ");
    Scanner scan = new Scanner(System.in);
    String word = scan.next();


    char[] chars = word.toCharArray(); 

    for(int i = 0; i < word.length(); i++)
    {

        System.out.println("---" + chars[i]);
        switch(chars[i])
        {
            case 0: sentence = " ";
                System.out.println("B");
                break;
            case 1: sentence = "A";
                break;
            case 2: sentence = "B";
                System.out.println("B");
                break;
            case 3: sentence = "C";
                break;


        }
        sentence+=sentence;
    System.out.println(sentence);
    }


}

}

如果我输入20 den它应该打印“B” 但它的打印为

Enter the word :
20
---2

---0

我哪里出错?

5 个答案:

答案 0 :(得分:3)

由于您正在进行char类型的切换,因此您的情况应该相同。在您的情况下,因为您将案例作为整数值,它只是不匹配。 '0'不等于0

switch(chars[i]) {
    case '0': // switch on char '0' and not integer 0.
    case '1': // switch on char '1' and not integer 1.
    case '2': // switch on char '2' and not integer 2.
    ...
}

答案 1 :(得分:2)

import java.io.IOException;
import java.util.Scanner;

public class Fh3 {
    public static void main(String args[]) throws IOException {

        String sentence = "";
        System.out.println("Enter the word : ");
        Scanner scan = new Scanner(System.in);
        String word = scan.next();

        //Switch case needs you to compare the expression with constants hence the final keyword. 
        final char CHARONE = '1';
        final char CHARTWO = '2';
        final char CHARTHREE = '3';
        final char CHARFOUR = '4';

        char[] chars = word.toCharArray();

        for (int i = 0; i < word.length(); i++) {
            System.out.println("---" + chars[i]);
            switch (chars[i]) {
                case 0:
                    sentence = " ";
                    System.out.println("B");
                    break;
                case CHARONE:
                    sentence = "A";
                    break;
                case CHARTWO:
                    sentence = "B";
                    System.out.println("B");
                    break;
                case CHARTHREE:
                    sentence = "C";
                    break;

            }
            sentence += sentence;
            System.out.println(sentence);
        }
    }
}

您试图将int与char进行比较..清除?

答案 2 :(得分:1)

因为您正在启用字符,而不是整数:

switch(chars[i]){
    case '0': sentence = " ";
           System.out.println("B");
           break;
    case '1': sentence = "A";
           break;
    case '2': sentence = "B";
           System.out.println("B");
           break;
     case '3': sentence = "C";
           break;
}

答案 3 :(得分:1)

您的交换机正在接受char但是没有合适的case。所以它只打印此语句System.out.println("---" + chars[i]);两次(因为word.length()在您的情况下返回2)< / p>

import java.io.*;
import java.util.*;

public class fh3

{
    public static void main(String args[])throws IOException

{

    String sentence = "";

    System.out.println("Enter the word : ");
    Scanner scan = new Scanner(System.in);
    String word = scan.next();


    char[] chars = word.toCharArray(); 

    for(int i = 0; i < word.length(); i++)
    {

        System.out.println("---" + chars[i]);
        switch(chars[i])
        {
            case '0': sentence = " ";
                System.out.println("B");
                break;
            case '1': sentence = "A";
                break;
            case '2': sentence = "B";
                System.out.println("B");
                break;
            case '3': sentence = "C";
                break;


        }
        sentence+=sentence;
    System.out.println(sentence);
    }


}

}

答案 4 :(得分:1)

在Java中,char类型通过Ascii table映射到int类型。

因此,如果你想检查char '0'而不是NUL char,你应该这样做:

switch(chars[i]) {
    case '0': // do the work
    case '1': // do the work
    // ...
}