-5

嗨,我想知道我的代码有什么问题,我得到了标题中所述的错误。这有什么问题?提前致谢。为什么我需要这么多细节,我觉得我已经描述得很好了。

import java.util.Scanner;
public class CombinationLock
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter three uppercase letters.");
        System.out.println("Hit enter after each letter.");
        String str1 = in.nextLine();
        String str2 = in.nextLine();
        String str3 = in.nextLine();
        System.out.println("Would you like to open the lock?");
        if(Yes)
            Scanner lol = new Scanner(System.in);
            System.out.print("Please enter the combination for this lock.");
            System.out.println("Hit enter after each letter");
            String str4 = lol.nextLine();
            String str5 = lol.nextLine();
            String str6 = lol.nextLine();
            if(str4.equals(str1))
                if (str5.equals(str2))
                    if(str6.equals(str3))
                        System.out.println("Congratulations you have unlocked the lock :)");
                    else
                        System.out.println("Sorry the combination you have input is not correct :/");
                else
                    System.out.println("Sorry the combination you have input is not correct :/");
            else
                System.out.println("Sorry the combination you have input is not correct :/");
        else
            System.out.println("This lock has been locked enter code here with the letters that you have just input.");
    }
}
4

5 回答 5

4

你必须使用{}

那里 :

    if(Yes)
        Scanner lol = new Scanner(System.in);
        System.out.print("Please enter the combination for this lock.");

此 if 语句仅适用于这一行Scanner lol = new Scanner(System.in);

所以最后一个 else 语句没有连接到任何 if 语句。

这就是你应该如何使用{}来获得正确的输出并清楚什么是什么:

    Scanner in = new Scanner(System.in);
    System.out.print("Please enter three uppercase letters.");
    System.out.println("Hit enter after each letter.");
    String str1 = in.nextLine();
    String str2 = in.nextLine();
    String str3 = in.nextLine();
    System.out.println("Would you like to open the lock?");
    if (Yes) {
        Scanner lol = new Scanner(System.in);

        System.out.print("Please enter the combination for this lock.");
        System.out.println("Hit enter after each letter");
        String str4 = lol.nextLine();
        String str5 = lol.nextLine();
        String str6 = lol.nextLine();
        if (str4.equals(str1)) {
            if (str5.equals(str2)) {
                if (str6.equals(str3)) {
                    System.out.println("Congratulations you have unlocked the lock :)");
                } else {
                    System.out.println("Sorry the combination you have input is not correct :/");
                }
            } else {
                System.out.println("Sorry the combination you have input is not correct :/");
            }
        } else {
            System.out.println("Sorry the combination you have input is not correct :/");
        }
    } else {
        System.out.println("This lock has been locked enter code here with the letters that you have just input.");
    }
于 2013-10-24T19:29:07.463 回答
3

{你在第一个之后缺少一个花括号if (Yes)

    if(Yes) {
        Scanner lol = new Scanner(System.in);
        System.out.print("Please enter the combination for this lock.");
        System.out.println("Hit enter after each letter");
        String str4 = lol.nextLine();
        String str5 = lol.nextLine();
        String str6 = lol.nextLine();
        if(str4.equals(str1))
            if (str5.equals(str2))
                if(str6.equals(str3))
                    System.out.println("Congratulations you have unlocked the lock :)");
                else
                    System.out.println("Sorry the combination you have input is not correct :/");
            else
                System.out.println("Sorry the combination you have input is not correct :/");
        else
            System.out.println("Sorry the combination you have input is not correct :/");
    }
    else
        System.out.println("This lock has been locked enter code here with the letters that you have just input.");
于 2013-10-24T19:28:36.383 回答
3

好吧,这是我的看法(摘自我之前的评论):

我对每个if 语句都使用大括号(快速前置/后置条件单行 if (..) throw .. 语句除外)。我认为自己是一位经验丰富的开发人员,我还建议初学者对每个if 语句都使用大括号。

遵循这样的建议可以防止以 ;-) 开头的语法错误

无论如何,紧随其后的是 Cruncher 说:

..“深度嵌套”通常被认为是不好的做法。如果您可以编写相同的代码,“浅”通常是首选。

所以,我想谈谈 if-else 语句的最后级联。事实上,为它们添加大括号并没有使代码更易于阅读。然而,是什么代码更容易调整方法,使其变得“更浅”。(它还删除了一些多余的行。)

// ..
if(Yes) {
    // ..
    if(str4.equals(str1)
       && str5.equals(str2)
       && str6.equals(str3)) {
         System.out.println("Congratulations you have unlocked the lock :)");
    } else {
         System.out.println("Sorry the combination you have input is not correct :/");
    }
} else {
    System.out.println("This lock has been locked enter code here with the letters..");
}
于 2013-10-24T19:45:49.573 回答
2

每个 if 块,如果该块包含多个语句,都应该用花括号括起来。如果是单条语句,还是推荐的。(注意:嵌套的 if 可以算作一条语句!这就是为什么无论技术上是否需要大括号,通常都应该使用大括号)您可以在每个 if 条件内嵌套额外的 if 块。

if(someCondition)
{
    //do some stuff
    if(someOtherCondition)
    {
        //do some additional stuff
    }
}
else
{
    //do some other stuff
}
于 2013-10-24T19:30:28.487 回答
0

从逻辑上讲,您在 if 之后缺少大括号。但即使你修复它,也没有变量叫做Yes. 如果您想始终运行它,请使用true

if(true) {
于 2013-10-24T19:32:47.437 回答