我遇到了一个java登录菜单应用程序的问题
问题描述:
我在大学有一个项目来创建一个登录菜单类型的应用程序。我遇到了一个java登录菜单应用程序的问题
我是一个非常初学者,所以请忍受我。 我可以请求某人指引我朝着正确的方向前进,因为我已经在这一点上留下了一些空白。
该应用程序没有完成,所以我知道会有更多的需求添加到它,只知道它只是一个非常准系统的应用程序,我期待构建。
选择选项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)
答
这是我对你的问题的办法。
首先,我将密码设置在第一位。
private static String correctPassword = "tommybee";
而且,我只能在这种情况下调用showMenu方法。
public static void main(String[] args) {
showMenu();
}
您不必在loginAttempt中调用showMenu方法,while语句也会被删除。
public static void loginAttempt() {
if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
System.out.println("Please change password and attempts first.");
}
if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
}
if (attemptsCounter >= 6) {
System.out.println("You have been locked out.");
}
}
在showMenu方法中,我只使用一个扫描仪类与系统的输入流。 检查the scanner class。
声明一个扫描器类和userChoice变量初始化为4.
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
这里是showMenu方法。
public static void showMenu()
// displays a menu and keeps displaying until user chooses the QUIT option
{
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
do {
System.out.println("Choose one of the list below");
System.out.println("1. Login");
System.out.println("2. Change Password");
System.out.println("3. Change Attempts");
System.out.println("4. Quit");
if(myScan.hasNextInt())
{
userChoice = myScan.nextInt();
switch (userChoice) {
case 1: {
System.out.println("You choose to login.");
System.out.println("Please enter a password.");
userPassword = myScan.next();
System.out.println("pass " + userPassword);
loginAttempt();
break;
}
case 2: {
System.out.println("You choose to change password.");
System.out.println("Please enter a new password.");
correctPassword = myScan.next();
break;
}
case 3: {
System.out.println("You choose to change attempts.");
System.out.println("Please enter amount of attempts.");
attemptsCounter = myScan.nextInt();
break;
}
case 4: {
System.out.println("You have quit the program.");
break;
}
default: {
System.out.println("Not a valid choice.");
}
}// closes switch
}
} while (myScan.hasNext() && userChoice != 4);
myScan.close();
System.out.println("Goodbye.");
}// closes showMenu1
我使用下一个方法,而不是nextInt方法。 查看api文档的next method。
在等待要扫描的输入此方法可能阻塞,
这里是我做了什么。
import java.util.Scanner;
public class LoginMenu {
private static String correctPassword = "tommybee";
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
{
Scanner myScan = new Scanner(System.in);
int userChoice = 4;
do {
System.out.println("Choose one of the list below");
System.out.println("1. Login");
System.out.println("2. Change Password");
System.out.println("3. Change Attempts");
System.out.println("4. Quit");
if(myScan.hasNextInt())
{
userChoice = myScan.nextInt();
switch (userChoice) {
case 1: {
System.out.println("You choose to login.");
System.out.println("Please enter a password.");
//Scanner myNewScan = new Scanner(System.in);
userPassword = myScan.next();
//myNewScan.close();
System.out.println("pass " + userPassword);
loginAttempt();
break;
}
case 2: {
System.out.println("You choose to change password.");
//Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter a new password.");
correctPassword = myScan.next();
//myNewScan.close();
break;
}
case 3: {
System.out.println("You choose to change attempts.");
//Scanner myNewScan = new Scanner(System.in);
System.out.println("Please enter amount of attempts.");
attemptsCounter = myScan.nextInt();
//myNewScan.close();
break;
}
case 4: {
System.out.println("You have quit the program.");
break;
}
default: {
System.out.println("Not a valid choice.");
}
}// closes switch
}
} while (myScan.hasNext() && userChoice != 4);
myScan.close();
System.out.println("Goodbye.");
}// closes showMenu1
public static void loginAttempt() {
//while ((correctPassword != userPassword) && (attemptsCounter >= 5)) {
if (!(userPassword.toLowerCase().equals(correctPassword)) && (attemptsCounter >= 5)) {
System.out.println("Please change password and attempts first.");
//showMenu();
}
if ((correctPassword.toLowerCase().equals(userPassword)) && (attemptsCounter <= 5)) {
System.out.println("You entered the correct password in " + attemptsCounter + " attempts");
}
if (attemptsCounter >= 6) {
System.out.println("You have been locked out.");
}
}
}
你知道为什么选择选项1返回运行时异常错误吗? –
什么是运行时异常细节? – tommybee