【问题标题】:Taking int input and parsing into char接受 int 输入并解析成 char
【发布时间】:2018-11-10 19:52:19
【问题描述】:

所以我的 main 2 天前被删除了,我的老师帮了我一些 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);

}

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

【问题讨论】:

  • 你的问题是?
  • while (repeat == true)while (repeat) 相同,while (test == true ^ lengthTest != false)while (test != !lengthTest) 相同,这似乎错误或过于复杂。
  • 我会将choice = Integer.toString(input.nextInt()).charAt(0) 替换为choice = input.nextInt()
  • 我也会打印任何异常,因为它可能对哪个很重要。
  • @PeterLawrey 是的,你是对的。通过删除test,我变得更加简单,现在我只是在做while (!lengthTest);

标签: java string char int


【解决方案1】:

您可以尝试使用 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;
        } 

【讨论】:

  • 成功了!我还添加了.toLowerCase(); 以忽略大写或小写
【解决方案2】:

您不能同时使用 int 和 chars,因为您只能使用一个变量并且变量必须具有类型,但是: 如果您将 char 或 Character 转换为 int,您将获得值。例如 ((int) 'e') 如果我没记错的话,计算结果为 101。 (试试 System.out.println((int) 'e')); 因此,在您的情况下,您可以切换 int 值并检测 1、2、3、4 和 101。 你的默认值也应该抛出一个异常,你很好。

快乐编码

【讨论】:

  • 假设用户在输入中输入 101,会退出代码吗?
  • 是的,那你就有问题了 :-) 这就是为什么在一个真实的项目中你应该做输入检查一个可能从一些语义枚举类型转换的原因。
  • 是的,如果从上面的解决方案中修复,我得到了它。将其更改为 String 并且可以正常工作
  • 是的,这是最好的解决方案 :)
【解决方案3】:

您可以只使用数字 1-4 的 char 表示:

char choice = input.next().charAt(0);
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;
} 

【讨论】:

  • char choice = input.nextInt().charAt(0); 没有用,它给了我Cannot invoke charAt(int) on the primitive type int
  • @Nuggethed2003 的意思是使用next(),而不是nextInt(),抱歉。已编辑和修复。
猜你喜欢
  • 2016-08-27
  • 2018-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2021-04-03
  • 2016-01-15
相关资源
最近更新 更多