爪哇;预期不是没有 if 的 else 语句

2024-05-15

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

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.");
    }
}

你必须使用{}!

There :

    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.");
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

爪哇;预期不是没有 if 的 else 语句 的相关文章

随机推荐