[英]Looping Java switch statement
My system is a banking system and I want my program to loop after the user has interacted with one of the processes (withdraw, view balance etc). 我的系统是银行系统,我希望我的程序在用户与其中一个流程(提款,查看余额等)进行交互之后循环执行。
After user get result I want to give a chance to continue with another process: 在用户得到结果之后,我想给一个机会继续另一个过程:
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");
}
}
I can see from the code you have already handled the requirement using end==false. 从代码中可以看到您已经使用end == false处理了需求。
You need to remove the return; 您需要删除退货; statement used after the switch case. 在切换大小写之后使用的语句。
Also, for case 1 use break; 另外,对于情况1,请使用break; instead of return; 而不是返回;
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");
}
You can also take input from the User whether he wants to continue or not and depending on the response, display the options again. 您还可以从用户那里获取他是否要继续的输入,并根据响应再次显示选项。
This here: 这里:
for (int i = 0; i < 3; i++) {
Around that other loop: 在另一个循环中:
} while (end == false)
is simply bogus. 简直是假的。
You want one loop that goes around your "menu"; 您想要一个围绕您的“菜单”的循环; and that loop ends when the user wants to end. 当用户想要结束时,该循环结束。
End of story. 故事结局。
just put all cases into a infinite loop and when user select case 4 then system.exit(0); 只需将所有案例置于无限循环中,然后当用户选择案例4时,则选择system.exit(0); will terminate program otherwise it will loop. 将终止程序,否则将循环。
import java.util.Scanner; 导入java.util.Scanner;
public class Test { 公开课测试{
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");
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.