【问题标题】:Constant Expression Required When Using an Array With a Switch Statement使用带有 switch 语句的数组时需要常量表达式
【发布时间】:2013-11-25 21:10:11
【问题描述】:

您好 StackOverflow 社区,我刚刚开始使用数组,我想知道如何使 switch 语句使用以下值。考虑到唯一的错误是需要常量表达式,这真的很烦人:

import java.lang.Character;

public class Ass11a {

    public static char[] vowel = new char[4];
    public static char[] consonant = new char[20];
    public static char[] number = new char[9];
    public static char[] punctuation = new char[10];
    public static void main(String[] args) {
        EasyReader console = new EasyReader();
        System.out.print("Enter a character: ");
        char input = console.readChar();
        Character.toLowerCase(input);
        vowel[0] = 'a';
        vowel[1] = 'e';
        vowel[2] = 'i';
        vowel[3] = 'o';
        vowel[4] = 'u';

        consonant[0] = 'q';
        consonant[1] = 'w';
        consonant[2] = 'r';
        consonant[3] = 't';
        consonant[4] = 'y';
        consonant[5] = 'p';
        consonant[6] = 's';
        consonant[7] = 'd';
        consonant[8] = 'f';
        consonant[9] = 'g';
        consonant[10] = 'h';
        consonant[11] = 'j';
        consonant[12] = 'k';
        consonant[13] = 'l';
        consonant[14] = 'z';
        consonant[15] = 'x';
        consonant[16] = 'c';
        consonant[17] = 'v';
        consonant[18] = 'b';
        consonant[19] = 'n';
        consonant[20] = 'm';

        number[0] = '1';
        number[1] = '2';
        number[2] = '3';
        number[3] = '4';
        number[4] = '5';
        number[5] = '6';
        number[6] = '7';
        number[7] = '8';
        number[8] = '9';
        number[9] = '0';

        punctuation[0] = '.';
        punctuation[1] = ',';
        punctuation[2] = '?';
        punctuation[3] = '!';
        punctuation[4] = ';';
        punctuation[5] = ':';
        punctuation[6] = '"';
        punctuation[7] = '\'';
        punctuation[8] = '(';
        punctuation[9] = ')';
        punctuation[10] = '-';                                                                                                                                               
        switch(input)
        {
            case vowel[0]:case vowel[1]:case vowel[2]:case vowel[3]:case vowel[4]:
                System.out.println("Vowel");
                break;
            case consonant[0]:case consonant[1]:case consonant[2]:case consonant[3]:
            case consonant[4]:case consonant[5]:case consonant[6]:case consonant[7]:
            case consonant[8]:case consonant[9]:case consonant[10]:case consonant[11]:
            case consonant[12]:case consonant[13]:case consonant[14]:case consonant[15]:
            case consonant[16]:case consonant[17]:case consonant[18]:case consonant[19]:
            case consonant[20]:
                System.out.println("Consonant");
                break;
            case number[0]:case number[1]:case number[2]:case number[3]:case number[4]:
            case number[5]:case number[6]:case number[7]:case number[8]:case number[9]: 
                System.out.println("Number"); 
                break;                              
            case punctuation[0]:case punctuation[1]:case punctuation[2]:case punctuation[3]:
            case punctuation[4]:case punctuation[5]:case punctuation[6]:case punctuation[7]:
            case punctuation[8]:case punctuation[9]:case punctuation[10]:       
                System.out.println("Punctuation");
                break;
            default:
                System.out.println("Other"); 
                break;              
        }
    }
}

【问题讨论】:

  • punctuation[0] 是常量表达式吗?不,所以你不能使用它。
  • @SotiriosDelimanolis 那么我该如何在保持 switch 语句的同时进行编辑呢?
  • 您没有在vowel[4] 获得IndexOutOfBounds 吗?
  • 你不能。您必须使用常量表达式。那是一个关键字,首先要查找它。
  • case vowel[0] 替换为case 'a' 等,因为这是常量(在运行时无法更改/编译器已经知道该值)

标签: java arrays char switch-statement


【解决方案1】:

你可以像这样使用enum

public enum Vowels {
  A,E,I,O,U;
  static boolean isVowel(char c) {
    switch (c) {
    case 'A': case 'a':
    case 'E': case 'e':
    case 'I': case 'i':
    case 'O': case 'o':
    case 'U': case 'u': return true;
    }
    return false;
  }
  static Vowels getVowel(char c) {
    switch (c) {
    case 'A': case 'a': return A;
    case 'E': case 'e': return E;
    case 'I': case 'i': return I;
    case 'O': case 'o': return O;
    case 'U': case 'u': return U;
    }
    return null;
  }
}

并像使用它

public static void main(String[] args) {
  Scanner console = new Scanner(System.in);
  while (console.hasNextLine()) {
    String input = console.nextLine();
    for (Character ch : input.toCharArray()) {
      if (Vowels.isVowel(ch)) {
        System.out.println(ch + " is vowel");
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2023-01-16
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多