如何在 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/