java - 将 int 输入解析为 char

标签 java string char int

所以我的 main 两天前被删除了,我的老师用 switch 代码帮助了我一些。我昨天重建了代码,他昨天离开了,无法帮助我。

public static void main(String[] args) throws InterruptedException {


    do {
        try {
            System.out.println("Enter your birthYear");
            birthYear = Integer.parseInt(input.next());
            int length = String.valueOf(birthYear).length();
            System.out.println(length);
            if (length != 4) {
                lengthTest = false;
                System.out.println("Invalid Choice");
            } else {
                lengthTest = true;

            }
            test = true;

        } catch (Exception e) {

            System.out.println("Invalid Choice");
        }

    } while (test == true ^ lengthTest != false);
    do {
        System.out.println("Please enter a number between 1-4 \n"
                + "1 = AreaOfTriangle \n" + 
                "----------------------------------\n" + 
                "2 = HoursToDaysAndHours Calculator \n" + 
                "---------------------------------- \n" + 
                "3 = CelciusToFahrenheit Calculator \n" + 
                "----------------------------------\n" + 
                "4 = BirthdayGame \r\n" + 
                "----------------------------------");



        try {
            choice = Integer.toString(input.nextInt()).charAt(0);
            System.out.println(choice);
        switch (choice) {
        case 1:
            aOT.areaOfTriangle();
            break;
        case 2:
            hTDAH.hoursToDaysAndHours();
            break;
        case 3:
            cTF.celciusToFahrenheit();
        case 4:
            System.out.println("Code not implemented");
            break;
        case 'e':
            repeat = false;
            break;
            default:
                System.out.println("");
                break;
        } 
        }catch (Exception e) {
            System.out.println("Invalid Awnser");
        }

    } while (repeat == true);

}

我的问题是在我的开关盒中,我希望能够同时使用 int 和 Char 。例如我想使用 e 退出和 4 个数字

最佳答案

您可以尝试使用 String 作为输入参数,那么任何 int 值或 char 都将被正确读取,无需转换它们:

      try {
           String choice = input.next();
           System.out.println(choice);
           switch (choice) {
           case "1":
               aOT.areaOfTriangle();
               break;
           case "2":
               hTDAH.hoursToDaysAndHours();
               break;
           case "3":
               cTF.celciusToFahrenheit();
           case "4":
               System.out.println("Code not implemented");
               break;
           case "e":
               repeat = false;
               break;
               default:
                   System.out.println("");
                   break;
        } 

关于java - 将 int 输入解析为 char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53242827/

相关文章:

java |两个JButton中的ActionListener,没有响应

java - 如果将字符串的值设置为包含对象的表达式会发生什么?

Javascript 字符串到数组/字典

char 数组和 char 指针与返回地址之间的冲突点

string - 在 OCaml 中将字符转换为字符串

java - 将 HashMap 的 HashMap 转换为 ArrayList 的 ArrayList

java - 这是合并排序的稳定实现吗

java - 使用 Jedis 的 Redis Key 过期通知

c++ - 给定仅由 '('和 ')'组成的字符串S,找出最长的有效子字符串的长度

检查字符串中是否存在所有 char 值