转载

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

如何在开关盒中使用角色?我将收到用户输入的第一个字母.
import javax.swing.*;

public class SwitCase {
    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");
        }
    }   
}

翻译自:https://stackoverflow.com/questions/6906001/how-do-i-used-a-char-as-the-case-in-a-switch-case

原文  https://codeday.me/bug/20190111/514138.html
正文到此结束
Loading...