java - 我在使用 java 登录菜单应用程序时遇到问题

标签 java authentication while-loop menu do-while

我在大学有一个项目来创建登录菜单类型的应用程序。

我是个初学者,所以请耐心等待。 我可以请求有人为我指出正确的方向吗,因为我在这方面遇到了一些空白。

该应用程序尚未完成,因此我知道还会有更多内容需要添加到其中,只需知道它只是我想要构建的一个非常准系统的应用程序即可。

选择选项 1 后,应用程序会告诉用户首先更改密码并尝试。更改密码和尝试次数(测试时我将其更改为 5)并重新选择选项 1 后,出现此错误 -

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - not a statement
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:74)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.loginAttempt(LoginMenu.java:77)
at loginmenu.LoginMenu.showMenu(LoginMenu.java:34)
at loginmenu.LoginMenu.main(LoginMenu.java:12)

这是代码 -

package loginmenu;

import java.util.Scanner;

public class LoginMenu {

    private static String correctPassword;
    public static String userPassword;
    public static int attemptsCounter;
    public static boolean loggedIn;

    public static void main(String[] args) {
        showMenu();
        loginAttempt();

    }

    public static void showMenu()
    // displays a menu and keeps displaying until user chooses the QUIT option
    {
        int userChoice;
        Scanner myScan = new Scanner(System.in);
        do {
            System.out.println("1.  Login");
            System.out.println("2.  Change Password");
            System.out.println("3.  Change Attempts");
            System.out.println("4.  Quit");
            userChoice = myScan.nextInt();

            switch (userChoice) {
            case 1: {
                System.out.println("You chose to login.");
                loginAttempt();
                break;
            }
            case 2: {
                System.out.println("You chose to change password.");
                Scanner myNewScan = new Scanner(System.in);
                System.out.println("Please enter a password.");
                userPassword = myNewScan.nextLine();

                break;
            }

            case 3: {
                System.out.println("You chose to change attempts.");
                Scanner myNewScan = new Scanner(System.in);
                System.out.println("Please enter amount of attempts.");
                attemptsCounter = myNewScan.nextInt();
                break;
            }
            case 4: {
                System.out.println("You have quit the program.");
                break;
            }
            default: {
                System.out.println("Not a valid choice.");
            }
            }// closes switch
        } while (userChoice != 4);
        myScan.close();
        System.out.println("Goodbye.");
    }// closes showMenu1

    public static void loginAttempt()
    {
        Scanner scan = new Scanner(System.in);

        while (correctPassword != userPassword) && (attemptsCounter>=5);
            {
                System.out.println("Please change password and attempts first."); 
                showMenu();
            }

        if (userPassword == correctPassword) && (attemptsCounter<=5)
            {
                System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
            }
        if (attemptsCounter>=6)
            {
                System.out.println("You have been locked out.");
            }
    }
}

最佳答案

修正括号并去掉分号,并且不要使用 ==/!= 作为字符串。

while (correctPassword != userPassword) && (attemptsCounter>=5);

应该是

while (!correctPassword.equals(userPassword) && (attemptsCounter>=5))

这里的括号有同样的问题:

if (userPassword == correctPassword) && (attemptsCounter<=5)

关于java - 我在使用 java 登录菜单应用程序时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46676714/

相关文章:

java - 从数据库同步实体管理器

java - 如何使用枚举生成 switch 语句框架

java - Java EE 项目的异常处理架构

java - RuntimeException:无法实例化 Activity 组件信息

javascript - JavaScript 中的 while(i) 循环

java - 如何将 Java 列表映射转换为 Scala 列表映射

java - java中的Bigtable认证

c# - 表单例份验证 : How to handle unauthorized authenticated user

c# - 从 MySql 获取数据到 C# 应用程序

Python:发生外部异常时如何正确继续while循环