【问题标题】:I cannot seem to append my else if statement我似乎无法附加我的 else if 语句
【发布时间】:2017-09-20 13:54:26
【问题描述】:

如果我尝试输入“oz”或“OZ”,else if 语句似乎没有继续,我不知道为什么。任何帮助,将不胜感激!谢谢!!!

 Scanner input = new Scanner(System.in);
    StringBuilder sb = new StringBuilder();
    String Ma = "mass" + "MASS";
    String La = "oz" + "OZ";

        System.out.println("Hi, what are you trying to find?");
        System.out.println("mass");
        System.out.println("vol");
        System.out.println("temp");
        System.out.println("sphere");
        System.out.println("density");
        String convert = input.nextLine();

        if (Ma.contains(sb.append(convert))) {
            System.out.println("You are trying to convert mass.");
            System.out.println("Type the unit of measurement you are trying to convert to.");
            System.out.println("If you are trying to find mass type mass.");
            System.out.println("pound");
            System.out.println("ounce");
            System.out.println("ton");
            System.out.println("gram");
            System.out.println("mass");
            String mass = input.nextLine();
        }
        else if (La.contains(sb.append(convert))) {
                System.out.println("34223412351Type the number of oz.");
                double oz = input.nextDouble();
                System.out.println(oz + " oz equal to " + oz / 16 + " lb.");
                System.out.println(oz + " oz equal to " + oz / 32000 + " ton.");
                System.out.println(oz + " oz equal to " + oz * 0.02835 + " kg.");
                System.out.println(oz + " oz equal to " + oz * 28.35 + " g.");
                System.out.println(oz + " oz equal to " + oz * 2835 + " cg.");
                System.out.println(oz + " oz equal to " + oz * 28350 + " mg.");
        }
    }
}

【问题讨论】:

    标签: if-statement append contains


    【解决方案1】:

    为什么需要stringbuilder可以直接使用Ma.contains(convert)和La.contains(convert),这样if else就可以正常运行了。

    在您的情况下,它没有为 oz 运行的原因是字符串生成器附加了 'oz' 两次。 1.当它检查看到在马 2.在拉检查时 所以在 La 你的 sb 包含 'ozoz',这就是 else if 永远不会运行的原因。

    如果您需要在此处使用字符串生成器,请在从扫描仪读取字符串后使用 sb.append,而不是在 if 条件中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2017-02-11
      • 1970-01-01
      相关资源
      最近更新 更多