【问题标题】:How do I stop else in if/else from being executed?如何阻止 if/else 中的 else 被执行?
【发布时间】:2017-06-25 06:46:24
【问题描述】:

我正在使用扫描仪方法让用户输入字符串中的一个单词,但即使用户输入了其中一个字符串,else 仍在执行。我该如何预防?

public static void main(String[] args) {         
    while(true) {
        StringBuffer StringBuffer = new StringBuffer();
        Scanner input = new Scanner(System.in);
        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 (String.valueOf(convert).contains("mass, volume, sphere, temp, density, pound, ounce, ton, gram,")) { 
            StringBuffer.append(String.valueOf(convert));
        } else {
            System.out.println("Wrong input. Try again.");
        }
    }
}

【问题讨论】:

  • 您不需要 String.valueOf(convert),只需进行转换即可。

标签: java string if-statement contains


【解决方案1】:

反过来,在您的变体字符串上调用contains。正如Clone Talk 提到的,你不需要String.valueOf,因为convert 已经是String(虽然它也可以使用。):

public static void main(String[] args) {
    while (true) {
        StringBuffer StringBuffer = new StringBuffer();
        Scanner input = new Scanner(System.in);
        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 ("mass, volume, sphere, temp, density, pound, ounce, ton, gram,".contains(convert)) {
            StringBuffer.append(convert);
        } else {
            System.out.println("Wrong input. Try again.");
        }
    }
}

解决 cmets:

为什么if(convert.contains("....")) 不起作用?

最简单的方法是查看the documentation of String.contains当且仅当此字符串包含指定的字符值序列时返回true。

这个字符串在问题的原始示例中(不在我的答案中)是convert,可以是"mass""volume"等.;

指定的字符值序列就是那个长字符串"mass, volume, ..."

那么,例如如何"mass" 可以包含"mass, volume, etc."?确实是相反的:"mass, volume, etc.".contains("mass") == true

HashSet.contains 会更高效

在此示例中,字符串看起来不会大到足以感受到性能提升,但一般来说,这是一个很好的点,特别是如果可能的输入数量不小并且为了可读性和可维护性。你可以这样:

// This part may vary:
private static String [] variants = {"mass", "volume", "sphere", "temp", "density", "pound", "ounce", "ton", "gram"};
private static Set<String> inputs = new HashSet<>(Arrays.asList(variants));

public static void main(String[] args) {
    while (true) {
        StringBuffer StringBuffer = new StringBuffer();
        Scanner input = new Scanner(System.in);
        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 (inputs.contains(convert)) {
            StringBuffer.append(convert);
        } else {
            System.out.println("Wrong input. Try again.");
        }
    }
}

【讨论】:

  • @cricket_007 在这个例子中可能不是。字符串太短。
  • O(1) 在哈希桶中查找的复杂性相比,线性搜索在各方面都更糟糕(除了代码长度,这不是一个收获),因此HashSet 几乎非常适合这个用例。不幸的是,这看起来像是一个家庭作业问题,在老师看来,使用标准库提供的工具可能并不可取。 @cricket_007
  • @zhelezoglo +1 解释。
猜你喜欢
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
相关资源
最近更新 更多