字符串和||语句

时间:2017-06-17 12:37:54

标签: java

你能用下面的代码帮助我找出我所遗漏的内容吗?我正在使用Eclipse。

Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    {
    if (input.equalsIgnoreCase(("front door")  || ("front") || ("basement") || ("basement entrance")))
        if (input.equalsIgnoreCase(("front door")  || ("front"))        
            System.out.println("Maggie went to the side of the home and open the basement door.  As the door opened, she could smell the dust from inside.");       
        else    
            if ((input.equalsIgnoreCase("basement") || ("basement entrance")))  
                System.out.println("Maggie walks up the steps and slowly opens the front door.");               
    else    
        System.out.println("That is not a correct answer");

2 个答案:

答案 0 :(得分:0)

这不是你使用逻辑或运算符的方式,你必须做类似

的事情
if (input.equalsIgnoreCase("front door") || input.equalsIgnoreCase("front") ||
    input.equalsIgnoreCase("basement") || input.equalsIgnoreCase("basement entrance")) {
    ...
}

答案 1 :(得分:0)

我认为有一些错误: 1. If-else逻辑:

  

a)if(input.equalsIgnoreCase((“front door”)||(“front”)||   (“地下室”)|| (“地下室入口”)))           if(input.equalsIgnoreCase((“front door”)||(“front”))

你在这里是什么意思?第二,如果是多余的。    b)

  

否则
              if((input.equalsIgnoreCase(“basement”)||(“basement entrance”)))

此部分无法访问,因为如果覆盖它。

  1. if(condition)中的语法错误。
  2. 假设它看起来应该是llke

    String input = scanner.nextLine();
            {
                input = input.toLowerCase();
    
            if (input.equals("front door")  || input.equals("front"))             
                    System.out.println("Maggie went to the side of the home and open the basement door.  As the door opened, she could smell the dust from inside.");       
            else if (input.equals("basement") || input.equals("basement entrance"))  
                        System.out.println("Maggie walks up the steps and slowly opens the front door.");               
            else    
                System.out.println("That is not a correct answer");