我已经包含在我的eDepot系统的登录功能中(使用Eclipse),但我不确定如何执行注销功能。我在研究它之后尝试了几个,但它似乎没有用。
这是我的代码:
package uk.ac.livjm.cms; //Name of package
import java.util.Scanner; //Imports scanner
import java.io.IOException; //Imports IOException
public class Depot { //Name of Program
static Scanner console = new Scanner(System.in);
@SuppressWarnings("unused")
public static void main(String[] args) throws IOException {
String User = "SFoster"; //Username of Manager 1
String Pass = "001"; //Password of Manager 1
String Username = "BSamuel"; //Username of Manager 2
String Password = "002"; //Password of Manager 2
String User1 = "1"; //Username of Driver 1
String Pass1 = "111"; //Password of Driver 1
String Username1 = "2"; //Username of Driver 2
String Password1 = "222"; //Password of Driver 2
String User2 = "3"; //Username of Driver 3
String Pass2 = "333"; //Password of Driver 3
String inputuser; // Input when user is typing their username in
String inputpass; // Input when user is typing their password in
int menu = 0; //Displays menu
int count = 0;
int input;
System.out.println(
"Are you a Manager or a Driver or are you Creating a new account?"
+ "\n"
+ "1. Manager"
+ "\n"
+ "2. Driver"
+ "\n"
+ "3. Creating a new account"); //Displays message
input = console.nextInt();
if (input == 3) {
DepotSystem.Account();
}
if (input == 1) { //Login for Manager
System.out.println("Managers Login"); //Displays message
System.out.println(
"Please Note: You will only be allowed to re-enter your Username and Password three times."); //Displays message
while (count < 3) // 3 menu options
{
System.out.println("Enter your Username:"); //Displays message
inputuser = console.next();
System.out.println("Enter your Password:"); //Displays message
inputpass = console.next();
if (inputuser.equals(User) && inputpass.equals(Pass)
|| (inputuser.equals(Username) && inputpass.equals(Password))) {
System.out.println(
"You have successfully logged in."
+ "\n"
+ "Welcome to the Managers Section!"
+ "\n"
+ "Enter the Number that corresponds to the option you wish to choose:");
System.out.println(
"1. Transfer available depot to another vehicle."
+ "\n"
+ "2. Set up new work schedule."
+ "\n"
+ "3. Search for available driver and vehicle."
+ "\n"
+ "4. Log out.");
menu = console.nextInt();
if (menu == 1) {
System.out.println("Transfer available depot to another vehicle.");
Transfer.transfer();
count = 0;
break;
} else if (menu == 2) {
System.out.println("Set up new work schedule.");
WorkSchedule WorkSchedule = new WorkSchedule();
uk.ac.livjm.cms.WorkSchedule.work();
count = 0;
break;
} else if (menu == 3) {
System.out.println("Search for available driver and vehicle.");
Vehicle Vehicle = new Vehicle();
uk.ac.livjm.cms.Vehicle.vehi();
count = 0;
break;
}
if (menu == 4) {
System.out.println("Log out.");
count = 0;
break;
} else {
System.out.println(menu + ": Invalid option.");
count++;
}
} else {
System.out.println("Incorrect Username or Password");
count++;
}
}
}
if (input == 2) {
System.out.println("Drivers Login");
System.out.println(
"Please Note: You will only be allowed to re-enter your Username and Password three times.");
while (count < 3) {
System.out.println("Enter your Username: ");
inputuser = console.next();
System.out.println("Enter your Password: ");
inputpass = console.next();
if (inputuser.equals(User1) && inputpass.equals(Pass1)
|| (inputuser.equals(Username1) && inputpass.equals(Password1))
|| (inputuser.equals(User2) && inputpass.equals(Pass2))) {
System.out.println(
"You have successfully logged in."
+ "\n"
+ "Welcome to the Drivers Section!"
+ "\n"
+ "Enter the Number that corresponds to the option you wish to choose: ");
System.out.println("1. View Work Schedule." + "\n" + "2. Log Out.");
menu = console.nextInt();
if (menu == 1) {
System.out.println("View Work Schedule.");
WorkScheduleDriver.workdriver();
count = 0;
break;
} else if (menu == 2) {
System.out.println("Log out.");
count = 0;
break;
} else {
System.out.println("Incorrect Username or Password");
count++;
}
}
}
}
}
}
答案 0 :(得分:1)
你的代码一团糟,这不是正确的方法,但根据你的代码,你有一个while循环,当count是&lt; 3并在注销部分而不是将计数设置为零,将其设置为3或任何其他数字,以便它从while循环中断,这将模拟注销。那是: -
T
你的代码中有很多不必要的中断语句,例如else if (menu == 2)
{
System.out.println("Log out.");
count = 3; // set to 3
}
,你可以再次设置count到3或者单独使用break而不是count = 0; break;
,这在那里是不必要的,而且您可以创建一个单独的登录方法,并通过减少几行代码与那里的usrename和密码进行比较。