用户将创建一个帐户,用户将登录。 用户可以创建多个帐户。 在密码或用户名中输入错误的登录输出错误。 我的问题是每当我创建一个帐户并登录时,它都不会读取我在创建帐户中输入的内容并强制用户在登录时输入有效的用户名和密码。
static JavaBank user = new JavaBank();
int accountNumber = 1000;
int initialDeposit;
String name;
private String username;
int pincode;
int verify;
public JavaBank(){
accountNumber = 1000;
name = "name";
username = "username";
pincode = 1000;
verify = 1000;
initialDeposit = 5000;
}
public JavaBank(int accountNumber, String username, String name, int pincode, int verify, int initialDeposit){
this.username = username;
this.accountNumber = accountNumber;
this.name = name;
this.pincode = pincode;
this.verify = verify;
this.initialDeposit = initialDeposit;
}
public static void main(String args[]){
while(true){
Scanner input = new Scanner(System.in);
String tempUsername;
int tempPass;
int counter = 0;
int option;
int valid1 = 1;
int valid;
int login = 0;
JavaBank newUser[] = new JavaBank[10];
System.out.println("\nJAVA BANK\n");
System.out.println("[1] Create Account");
System.out.println("[2] Login Account");
System.out.println("[3] Exit\n");
System.out.print("Option: ");
option = input.nextInt();
switch(option){
case 1:
System.out.println("\n[1] Create Account\n");
System.out.println("Account Number: "+user.accountNumber);
System.out.print("Account Name: ");
user.name = input.next();
System.out.print("Username: ");
user.username = input.next();
do{
valid = 0;
System.out.print("Pincode: ");
user.pincode = input.nextInt();
if(user.pincode < 1000 || user.pincode > 9999){
valid = 1;
System.out.println("Pincode should be 4 digit! Please try again!");
continue;
}
System.out.print("Verify Pincode: ");
user.verify = input.nextInt();
if(user.verify < 1000 || user.verify > 9999){
valid = 1;
System.out.println("Pincode should be 4 digit! Please try again!");
continue;
}
if(user.pincode != user.verify){
valid = 1;
System.out.println("Pincode does not match! Please try again!");
}
}while(valid!=0);
do{
valid = 0;
System.out.print("Initial Deposit[Min. of 5000]: ");
user.initialDeposit = input.nextInt();
if(user.initialDeposit <5000){
valid = 1;
System.out.println("You entered less than 5000! Please try again!");
}
System.out.println("\nCongratulations!");
System.out.println("Your account has been created succesfully!");
}while(valid!=0);
newUser[counter] = new JavaBank(user.accountNumber++, user.username, user.name, user.pincode, user.verify, user.initialDeposit);
counter++;
break;
case 2:
do{
System.out.print("Username: ");
tempUsername = input.next();
System.out.print("Pincode: ");
tempPass = input.nextInt();
for(int index=0; index<counter; index++){
if(tempUsername.equals(newUser[index].username)){
if(tempPass == newUser[index].pincode)
{
valid1 = 0;
login = index;
}
}
}
if(valid1!=0){
System.out.println("Username or password does not exist. Please try again!");
}
}while(valid1!=0);
答案 0 :(得分:1)
您需要将user[]
和counter
从main()
移动为静态变量。
public class JavaBank {
static JavaBank user = new JavaBank();
int accountNumber = 1000;
int initialDeposit;
String name;
private String username;
int pincode;
int verify;
private static int counter = 0;//Add here and remove from main()
private static JavaBank newUser[] = new JavaBank[10];//add here and remove from main()
public JavaBank() {
accountNumber = 1000;
name = "name";
username = "username";
pincode = 1000;
verify = 1000;
initialDeposit = 5000;
}
.
.
.
这是怎么回事。您递增计数器,再次输入选项时,计数器值和用户将丢失。当你将它们设为static
时,它就可用,直到程序存在。