如果声明,我似乎无法追加我的其他

时间:2017-04-24 02:02:29

标签: if-statement append contains

如果我尝试键入“oz”或“OZ”,则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.");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

为什么你需要你可以直接使用Ma.contains(转换)和La.contains(转换)的stringbuilder,这将正确运行if else。

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

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