我的系统是一个银行系统,我希望我的程序在用户与其中一个进程(撤销,查看余额等)进行交互后循环。
用户获得结果后,我希望有机会继续其他流程:
package smartcode.atm.h.w;
import java.util.Scanner;
public class SmartCodeATMHW {
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
return;
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}
答案 0 :(得分:0)
我可以从代码中看到你已经使用end == false来处理需求。
您需要删除退货;在切换案例后使用的语句。
另外,对于案例1使用break;而不是返回;
public static void main(String[] args) {
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
do{
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
break;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}while (end == false);
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
您也可以从用户那里获取输入,无论他是否想要继续,并根据响应,再次显示选项。
答案 1 :(得分:0)
这里:
for (int i = 0; i < 3; i++) {
围绕那个其他循环:
} while (end == false)
简直就是虚假。
你希望一个循环围绕你的“菜单”;当用户想要结束时,该循环结束。
故事结束。
答案 2 :(得分:-1)
将所有案例置于无限循环中,当用户选择案例4时,则系统.exit(0);将终止程序,否则它将循环。
import java.util.Scanner;
公共课测试{
public static void main(String [] args){
int p;
int y = 1234;
int Amount = 500;
int result;
int info;
int f;
int g;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Please enter PIN Number ");
p = in.nextInt();
if (p == y) {
System.out.println("You have entered correct PIN number \n\n*****************");
profile pr = new profile();
pr.setName("Prapashtica");
System.out.println("Welcome mr." + pr.getName());
//loop from here
boolean end = false;
while(true){
System.out.println("\nPress 1 to View Bank Account");
System.out.println("Press 2 to Deposit money");
System.out.println("Press 3 to Withdraw money");
System.out.println("Press 4 to Cancle the Process \n\n********************");
info = in.nextInt();
switch (info) {
case 1:
System.out.println("You have " + Amount + "$ available");
return;
case 2:
System.out.println("You have " + Amount + "$ available");
System.out.println("Please enter the amount you wish to deposit");
f = in.nextInt();
result = Amount + f;
System.out.println("You have " + result + "$ available");
break;
case 3:
for (int l = 0; l < 3; l++) {
System.out.println("You have " + Amount + " $ available");
System.out.println("Please eter the amount you wish to withdraw");
g = in.nextInt();
if (g > 500) {
System.out.println("You cannot withdraw more than your current amount");
} else {
System.out.println("You have succesfully withdrawed " + g + " $");
result = Amount - g;
System.out.println("You now have " + result + " $ available");
}
}
System.out.println("Your card is now blocked");
break;
case 4:
System.out.println("You have canceled the proccess");
System.exit(0);
break;
default:
System.out.println("Error \nInvalid number");
break;
}
}
} else {
System.out.println("You have entered incorrect PIN number \nPlease try again \n*******************************");
}
}
System.out.println("Your card is now blocked");
}
}