【问题标题】:I am trying to compare strings, but it is not recognizing我正在尝试比较字符串,但无法识别
【发布时间】:2014-04-12 23:40:14
【问题描述】:

所以我尝试使用 if 语句检查字符串,我创建了一个测试计算机,它根据输入给出预定的响应,唯一有效的输入恰好是 hi 或 hello,带空格的字符串似乎没有工作,这是为什么呢?

import java.util.Scanner;
public class ComputaBot {


public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String text;
    String computer = "ComputaBot:";
    String[] input = {
            "how are you?", "hi", "hello", "what is your name",
            "do you like chicken?", "where are you from?",
            "do you like cheese?"
    };

    do{
        System.out.println("Type to talk!:");
        text = scan.next();

    if (text.equals(input[1]) || text.equals(input[2])) {
        System.out.println(computer + " " +"Hello!");

    }else if (text.equalsIgnoreCase(input[0])) {
        System.out.println(computer + " " +"I'm fine, you?!");
    }else if (text.equals(input[3])) {
        System.out.println(computer + " " +"Jimmy");
    }else if (text.equals(input[4])) {
        System.out.println(computer + " " +"Yes! Love it");
    }else if (text.equals(input[5])){
        System.out.println(computer + " " +"Germany");
    }else if (text.equals(input[6])){
        System.out.println(computer + " " +"only on pizza");
    }else 
        System.out.println("bye");
    }while(!text.equals("bye"));

    scan.close();
}
 }

【问题讨论】:

  • 因为scan.next() 只能得到一个字。

标签: java string if-statement


【解决方案1】:

next() 方法读取一个单词。你应该使用nextLine(),它读取整行(由按下Enter时输入的换行符分隔):

text = scan.nextLine();

来自JavaDocs

public String next():从这个扫描器中查找并返回下一个完整的令牌。一个完整的标记前后是匹配分隔符模式的输入。

【讨论】:

  • 不要忘记将答案也改为小写。
  • @alvonellos 由equalsIgnoreCase() 处理。
  • 哦!我的眼睛完全跳过了那条线(可能是因为我在寻找其他东西)不过答案很好。你就像一个忍者,这是我看到你在不到一分钟内回复的第二个帖子——应该有一个类似的徽章。
【解决方案2】:

使用

scan.nextLine() 

而不是

scan.next()

【讨论】:

    【解决方案3】:

    Scan.next() 仅读取下一个单词,您希望 scan.nextLine() 读取完整行。如果您使用 Map 将所有问题/答案成对添加会更好,这样您就不必执行所有 if/then/else 语句。

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class ComputaBot {
    
       public ComputaBot() {
       }
    
       public static void main(String[] args) {
          Scanner scan = new Scanner(System.in);
          String text;
          String computer = "ComputaBot:";
          Map<String,String> bot = new HashMap<String,String>();
          bot.put("how are you?", "I'm fine, you?!");
          bot.put("how are you?", "Hello!");
          bot.put("hi", "Hello!");
          bot.put("what is your name", "Jimmy");
          bot.put("do you like chicken?", "Yes! Love it");
          bot.put("where are you from?", "Germany");
          bot.put("do you like cheese?", "only on pizza");
    
          do {
             System.out.println("Type to talk!:");
             text = scan.nextLine();
    
             String answer = bot.get(text.toLowerCase());
             System.out.println(computer + " " + answer);
          } while (!text.equalsIgnoreCase("bye"));
          scan.close();
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多