我在大学里有一个项目来创建登录菜单类型应用程序。
我是一个非常初学者,所以请耐心等待。我可以要求有人指出我正确的方向吗,因为我在这个问题上有点空白。
该应用程序尚未完成,因此我知道会有更多需要添加的内容,只需知道它只是我想要构建的一个非常准系统的应用程序。
选择选项 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();
}
您不必在loginTry中调用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 方法中,我只将一个扫描器类与系统的输入流一起使用。检查扫描仪类。
声明一个 Scanner 类并将 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
我使用 next 方法而不是 nextInt 方法。请参阅 api 文档的下一个方法。
此方法可能会在等待输入扫描时阻塞,
这是我所做的。
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.");
}
}
}