JAVA;如果没有,预计不会发表其他声明 [英] JAVA ; expected not a statement else without if

查看:72
本文介绍了JAVA;如果没有,预计不会发表其他声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I was wondering what's wrong with my code I get the errors stated in the title. What is wrong with this? Thanks in advance. Why do I need so many details, I feel like I've described it well enough.

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


推荐答案

你必须使用 {}

那里:

    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);

This if-statement is only for this line Scanner lol = new Scanner(System.in);

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

So the last else-statement is not connected to any if-statement.

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

This is how you should use {} for getting the right output and to be clear what is for what :

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

这篇关于JAVA;如果没有,预计不会发表其他声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆