1

我正在从用户那里获取字符串中的输入,我想使用 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

我哪里错了?

4

5 回答 5

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.
    ...
}
于 2013-10-24T09:32:14.443 回答
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 .. Clear 进行比较?

于 2013-10-24T10:12:05.693 回答
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;
}
于 2013-10-24T09:32:08.323 回答
1

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

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);
    }


}

}
于 2013-10-24T09:32:29.867 回答
1

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

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

switch(chars[i]) {
    case '0': // do the work
    case '1': // do the work
    // ...
}
于 2013-10-24T09:33:00.210 回答