用户名和密码登录java项目
我试图编写一个程序,让您选择,如果你想注册或登录,如果选择作者明智,它会让你知道,你只能选择注册或登录... 我有一个问题,即程序有时会停留在循环中,这告诉我我输入了错误的输入,尽管我输入了“登录”并且无法退出该循环。 我得到的另外一个问题是登录代码: 我添加了user_id,它等于用户count+1
,我想检查用户和通过登录的用户是否都正确使用for循环的用户,并检查用户输入是否等于每个用户的密码user_id
,我只是不知道该怎么做我想也许给任何用户的对象与计数,所以我可以检查用户一通过一个在我的for循环user_id.username
和user_id.password.
用户名和密码登录java项目
import java.util.Scanner; public class users {
public String user_name;
public int user_id = 1;
private String password;
public static int count = 1;
public static String input;
public users(String Ruser, String Rpassword) {
this.user_id = count++;
this.user_name = Ruser;
this.password = Rpassword;
count++;
System.out.printf("User %s has been crated \n", Ruser);
System.out.printf("Enter 'login' to log in or 'register' to open another account");
}
public static void login(String Luser, String Lpassword) {
for (int i = 1; i <= count; i++) {
System.out.printf("Enter 'login' to log in or 'register' to open another account");
// user_id.users
// if(this.user_name)
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("login");
System.out.println("register");
input = scanner.nextLine();
while (input.equals("login")) {
System.out.println("username");
String Luser = scanner.nextLine();
System.out.println("Password");
String Lpassword = scanner.nextLine();
int a = count;
login(Luser, Lpassword);
System.out.println("");
input = scanner.nextLine();
}
while (input.equals("register")) {
System.out.println("username");
String Ruser = scanner.nextLine();
System.out.println("Password");
String Rpassword = scanner.nextLine();
users count = new users(Ruser, Rpassword);
System.out.println("");
input = scanner.nextLine();
}
while ((!input.equals("register")) || (!input.equals("login"))) {
System.out.println("invild option, chose login or regiser!");
input = scanner.nextLine();
}
回答:
在main方法我会做这样的事情:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("login");
System.out.println("register");
while(true) {
input = scanner.nextLine();
switch(input) {
case "exit":
System.exit(0);
break;
case "login":
System.out.println("username");
String Luser = scanner.nextLine();
System.out.println("Password");
String Lpassword = scanner.nextLine();
int a = count;
login(Luser, Lpassword);
System.out.println("");
// input = scanner.nextLine();
break;
case "register":
System.out.println("username");
String Ruser = scanner.nextLine();
System.out.println("Password");
String Rpassword = scanner.nextLine();
Users count = new Users(Ruser, Rpassword);
System.out.println("");
// input = scanner.nextLine();
break;
default:
System.out.println("invild option, chose login, regiser or exit!");
continue;
}
}
以上是 用户名和密码登录java项目 的全部内容, 来源链接: utcz.com/qa/266768.html