我尝试编写一个程序,让您选择是否要注册或登录,如果选择作者明智,它会让您知道您只能选择注册或登录...
我有一个问题,程序有时卡在循环中,告诉我输入了错误的输入,虽然我键入“登录”,你不能摆脱这个循环。
我得到的另一个问题是登录代码:
我添加了等于用户count+1
的user_id,我想检查登录中的用户和传递是否正确,并且for循环覆盖用户数量并检查用户是否输入等于user_id
的每个用户的密码,我只是不知道怎么做我想也许给任何用户的对象计数,所以我可以在我的用户逐个检查用户for for user_id.username
和user_id.password.
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();
}
答案 0 :(得分:0)
在主要方法中,我会做这样的事情:
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;
}
}