java - 如何在 switch-case 中使用 char 作为 case?

标签 java switch-statement character

如何在 switch-case 中使用字符?无论用户输入什么,我都会得到第一个字母。

import javax.swing.*;

public class SwitchCase {
    public static void main (String[] args) {
        String hello = "";
        hello = JOptionPane.showInputDialog("Input a letter: ");
        char hi = hello;
        switch(hi) {
            case 'a': System.out.println("a");
        }
    }   
}

最佳答案

public class SwitCase {
    public static void main(String[] args) {
        String hello = JOptionPane.showInputDialog("Input a letter: ");
        char hi = hello.charAt(0); // get the first char.
        switch(hi) {
            case 'a': System.out.println("a");
        }
    }   
}

关于java - 如何在 switch-case 中使用 char 作为 case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6906001/

相关文章:

java - 流多个过滤器内部

c# - 为什么在 switch case 语句中只允许使用有限的类型

javascript - 如何让输入字母时出现图像?

java - switch case 中的枚举引用

java - 为什么我的父类(super class)的字段在这里被初始化?

java - Android JSONReader 未找到类

C简单数组访问比较

html 中 SMS TextArea 的 Javascript 字符计数器

c - 在C中按字典顺序排序

java - Selenium java测试NG : Is it possible to use dataprovider data partially in testng test method