Hey Guys编写了一个程序,其中包含一个服务菜单,就像银行卡一样。 问题是我使用了一个名为doLogin的方法并使用了一个布尔值,因此如果answer == true将是doServiceMenu,如果它是假的,我希望它继续获取doLogin。
然而,这不起作用,我不确定这是否是我的逻辑,任何反馈将不胜感激
`enter code here`import java.util.Scanner;
class TopUp{
//class variable
static Scanner scan = new Scanner (System.in);
static String name = "Joe";
static String cardNumber = "1234 5678 9101 1213";
static double balance = 0;
public static void main(String[] args){
boolean isLoginCorrect = false;
isLoginCorrect = doLogin();
if(isLoginCorrect == true){
doServiceMenu();
}
else if(isLoginCorrect == false){
System.out.println("Incorrect username or password,try again!");
System.out.println();
doLogin();
}
}
public static boolean doLogin(){
String userName = "joe" ;
String password = "123456";
String nameInput;
String pwdInput;
System.out.println("Please enter your Username: ");
nameInput = scan.nextLine();
System.out.println("Please enter your Password: ");
pwdInput = scan.nextLine();
return ((userName.equals(nameInput)) && (password.equals(pwdInput)));
}
public static void doServiceMenu(){
System.out.println("<********************* Services Menu ****************>");
System.out.println("<************** Please Select an option: ***********>");
System.out.println("1> Customer Details");
System.out.println("2> Customer Balance");
System.out.println("3> Top-up");
System.out.println("4> Make A Purchase");
System.out.println("5> Quit");
System.out.println("");
System.out.println("<*************************************************>");
Scanner keyboard = new Scanner(System.in);
String menuSelect = keyboard.nextLine();
switch (menuSelect) {
case "1":
showCustomerDetails();
checkIfUserWantsAnotherTransaction();
break;
case "2":
printBalance();
checkIfUserWantsAnotherTransaction();
break;
case "3":
doTopUp();
checkIfUserWantsAnotherTransaction();
break;
case "4":
makePurchase();
checkIfUserWantsAnotherTransaction();
break;
case "5":
System.out.println("Goodbye!!!");
break;
default:
System.err.println ( "Unrecognized option" );
break;
}
}
public static void showCustomerDetails(){
System.out.println("Name: " + name);
System.out.println("Card Number : " + cardNumber);
}
public static void printBalance(){
System.out.println("Your balance is : " + balance);
}
public static void increaseBalance(double amount){
balance += amount;
}
public static void decreaseBalance(double amount){
balance -= amount;
}
public static void doTopUp(){
System.out.println("Please enter a Top Up Amount: ");
double amount = scan.nextDouble();
double maxTopUp = 1000 - balance;
if(amount > maxTopUp){
System.out.println("Amount exceedes Max Top Up: ");
}
else{
increaseBalance(amount);
printBalance();
}
}
public static void makePurchase(){
System.out.println("Please enter the purchase amount: ");
double amount = scan.nextDouble();
double purchase = amount - balance;
if(amount > balance){
System.out.println("Sorry Insufficent Funds: ");
}
else{
decreaseBalance(amount);
printBalance();
}
}
public static void checkIfUserWantsAnotherTransaction() {
System.out.println("Would you Like another Service: y/n");
Scanner keyboard = new Scanner(System.in);
String option = keyboard.nextLine();
if(option.equals("y")){
doServiceMenu();
}
else if(option.equals("n")){
System.out.println("Goodbye!!!");
}
}
}
答案 0 :(得分:0)
如果他们第一次登录失败,程序将进入此栏:
else if(isLoginCorrect == false){
System.out.println("Incorrect username or password,try again!");
System.out.println();
doLogin();
}
在该块结束时,程序退出(无论登录是否成功)。你需要在doLogin()周围引入一个循环,当它最终成功时,调用doServiceMenu()
函数。
我会实现这样的事情:
while(doLogin() != true) {
System.out.println("Incorrect username or password,try again!");
}
doServiceMenu();
答案 1 :(得分:0)
如果您遇到编译错误,则下列代码可能无效:
String menuSelect = keyboard.nextLine();
switch (menuSelect) {
...
}
您无法切换字符串值。您只能切换int或Enums。所以你应该首先将你的String转换为int。
就像那样:
int menuSelect = Integer.valueOf(keyboard.nextLine());
switch (menuSelect) {
case 1:
...
}
答案 2 :(得分:0)
在main
:
boolean isLoginCorrect = false;
int maxAttempt = 5;
for (int loginAttempt = 1; (isLoginCorrect) || (loginAttempt > maxAttempt); loginAttempt++)
{
isLoginCorrect = doLogin();
if ((!isLoginCorrect) && (loginAttempt < maxAttempt))
{
System.out.println("Incorrect username or password,try again!");
System.out.println();
}
}
if(isLoginCorrect == true){
doServiceMenu();
}