我不是最好的程序员,所以如果你能向我解释为什么以及我应该做些什么来解决这个问题,我将不胜感激。
当我运行程序时,我能够输入所有内容,但最后它突出显示了第一个带有java.lang.stackoverflowError的扫描仪行。
import java.util.Scanner;
import java.lang.Object;
public class Login
{
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
public void createAccount(){
System.out.print("Create a 6 digit account number: ");
int createNumber = input.nextInt();
System.out.print("Create a 4 digit PIN: ");
int createPin = input2.nextInt();
Login acc = new Login();
acc.loginScreen(createNumber, createPin);
}
public void loginScreen(int createNumber, int createPin){
System.out.println("Enter your acocunt number: ");
int accEntryNumber = input.nextInt();
int lengthNum = String.valueOf(accEntryNumber).length();
System.out.println("Enter your pin: ");
int accEntryPin = input2.nextInt();
int lengthPin = String.valueOf(accEntryPin).length();
if(accEntryPin == createPin && accEntryNumber == createNumber){
Main access = new Main();
access.mainMenu();
}
else{
System.out.println("Access denied");
}
}
}
答案 0 :(得分:-1)
您可以为整个程序使用一台扫描仪。像这样:
import java.util.Scanner;
import java.lang.Object;
public class Login
{
Scanner input = new Scanner(System.in);
public void createAccount(){
System.out.print("Create a 6 digit account number: ");
int createNumber = input.nextInt();
System.out.print("Create a 4 digit PIN: ");
int createPin = input.nextInt();
Login acc = new Login();
acc.loginScreen(createNumber, createPin);
}
public void loginScreen(int createNumber, int createPin){
System.out.println("Enter your acocunt number: ");
int accEntryNumber = input.nextInt();
int lengthNum = String.valueOf(accEntryNumber).length();
System.out.println("Enter your pin: ");
int accEntryPin = input.nextInt();
int lengthPin = String.valueOf(accEntryPin).length();
if(accEntryPin == createPin && accEntryNumber == createNumber){
Main access = new Main();
access.mainMenu();
}
else{
System.out.println("Access denied");
}
}
}