我正在为我的案例研究制作ATM机程序。如何在用户按下取消后返回到主菜单(具有余额查询等)的第一个输出(例如我还没有编码)。
来自评论:"如果用户完成退出,我该如何显示相同的窗口(这是主菜单)。"
import javax.swing.JOptionPane;
import java.util.Scanner;
public class casestudy
{
public static void main (String Args [])
{
Scanner sc=new Scanner (System.in);
String acct[]={"0123-4567-8901", "2345-6789-0123", "3456-7890-1234","4567-8901-2345", "5678-9012-3456"};
String name[]={"Dina Balle", "Mally Gaya", "Toto Lee", "Mina Mahal", "Cory Pot"};
int bal[]={5000, 0, 10000, 2500, 10000};
int pinarr []={0000, 1111, 2222, 3333, 4444, 5555};
int wdamount=0;
int depamount =0;
int pinattempt=0;
int wrongall=0;
boolean correct = false;
boolean wrong = false;
String g=JOptionPane.showInputDialog ("WELCOME TO FILIPINAS BANKING CORPORATION!\n\nPress [C] if you are a CUSTOMER, [A] if you are an ADMINISTRATOR: ");
char level = g.charAt(0);
switch (level)
{
case 'A':
case 'a': JOptionPane.showMessageDialog(null, "HEHE");break;
default : JOptionPane.showMessageDialog(null, "INVALID CODE!"); System.exit(0); break;
case 'C':
case 'c':
{
String a=JOptionPane.showInputDialog ("FILIPINAS BANKING CORPORATION\n===========================================\nPress [S] to START TRANSACTION and [Q] to QUIT: ");
char choice = a.charAt(0);
switch (choice)
{
case 'Q':
case 'q': JOptionPane.showMessageDialog(null,"The program will now exit"); System.exit(0); break;
default : JOptionPane.showMessageDialog(null,"INVALID SELECTION!"); System.exit(0); break;
case 'S':
case 's':
JOptionPane.showMessageDialog (null, "====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\n\n\t\t\tSTART TRANSACTION!");
while ((pinattempt!=3)&&(correct=true))
{
int pin=Integer.parseInt (JOptionPane.showInputDialog ("Enter your PIN: "));
switch (pin)
{
case 1111:
{
pinattempt=3; correct=true;
String b=JOptionPane.showInputDialog("====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\t\t\tSELECT TYPE OF TRANSACTION\n\t\t[B] - BALANCE INQUIRY\n\t\t[W] - WITHDRAWAL\n\t\t[D] - DEPOSIT \n\t\t[C] - CANCEL");
char pick=b.charAt(0);
switch (pick)
{
case 'B':
case 'b': JOptionPane.showMessageDialog(null, "Account #: " +acct [0]+"\n"+"Account Name: " +name [0]+"\n"+"Balance: "+bal[0],"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);System.exit(0); break;
case 'W':
case 'w':
String q =JOptionPane.showInputDialog("Enter Amount to be WITHDRAWN: ");
wdamount = Integer.parseInt(q);
if ((wdamount<100)||(wdamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [0]+"\n"+"The current BALANCE is now: " +(bal[0]-wdamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE); break;
case 'D':
case 'd':
String l = JOptionPane.showInputDialog("Enter amount to be DEPOSITED: ");
depamount = Integer.parseInt(l);
if ((depamount<100)||(depamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [0]+"\n"+"The current BALANCE is now: " +(bal[0]+depamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);
break;
}
}break;
case 2222:
{
pinattempt=3; correct=true;
String c=JOptionPane.showInputDialog("====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\t\t\tSELECT TYPE OF TRANSACTION\n\t\t[B] - BALANCE INQUIRY\n\t\t[W] - WITHDRAWAL\n\t\t[D] - DEPOSIT \n\t\t[C] - CANCEL");
char pick=c.charAt(0);
switch (pick)
{
case 'B':
case 'b': JOptionPane.showMessageDialog(null, "Account #: " +acct [1]+"\n"+"Account Name: " +name [1]+"\n"+"Balance: "+bal[1],"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);System.exit(0); break;
case 'W':
case 'w':
if (bal[1]<=0)
{
JOptionPane.showMessageDialog(null, "You cannot withdraw because you have "+bal[1]+" balance.");
System.exit(0);
}
String q =JOptionPane.showInputDialog("Enter Amount to be WITHDRAWN: ");
wdamount = Integer.parseInt(q);
if ((wdamount<100)||(wdamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
}
}break;
case 3333:
{
pinattempt=3; correct=true;
String d=JOptionPane.showInputDialog("====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\t\t\tSELECT TYPE OF TRANSACTION\n\t\t[B] - BALANCE INQUIRY\n\t\t[W] - WITHDRAWAL\n\t\t[D] - DEPOSIT \n\t\t[C] - CANCEL");
char pick=d.charAt(0);
switch (pick)
{
case 'B':
case 'b':
JOptionPane.showMessageDialog(null, "Account #: " +acct [2]+"\n"+"Account Name: " +name [2]+"\n"+"Balance: "+bal[2],"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);System.exit(0); break;
case 'W':
case 'w':
String q =JOptionPane.showInputDialog("Enter Amount to be WITHDRAWN: ");
wdamount = Integer.parseInt(q);
if ((wdamount<100)||(wdamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [2]+"\n"+"The current BALANCE is now: " +(bal[2]-wdamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE); break;
case 'D':
case 'd':
String l = JOptionPane.showInputDialog("Enter amount to be DEPOSITED: ");
depamount = Integer.parseInt(l);
if ((depamount<100)||(depamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [2]+"\n"+"The current BALANCE is now: " +(bal[2]+depamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);
break;
}
}break;
case 4444:
{
pinattempt=3; correct=true;
String e=JOptionPane.showInputDialog("====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\t\t\tSELECT TYPE OF TRANSACTION\n\t\t[B] - BALANCE INQUIRY\n\t\t[W] - WITHDRAWAL\n\t\t[D] - DEPOSIT \n\t\t[C] - CANCEL");
char pick=e.charAt(0);
switch (pick)
{
case 'B':
case 'b':
JOptionPane.showMessageDialog(null, "Account #: " +acct [3]+"\n"+"Account Name: " +name [3]+"\n"+"Balance: "+bal[3],"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);System.exit(0); break;
case 'W':
case 'w':
String q =JOptionPane.showInputDialog("Enter Amount to be WITHDRAWN: ");
wdamount = Integer.parseInt(q);
if ((wdamount<100)||(wdamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [3]+"\n"+"The current BALANCE is now: " +(bal[3]-wdamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE); break;
case 'D':
case 'd':
String l = JOptionPane.showInputDialog("Enter amount to be DEPOSITED: ");
depamount = Integer.parseInt(l);
if ((depamount<100)||(depamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [3]+"\n"+"The current BALANCE is now: " +(bal[3]+depamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);
break;
}
}break;
case 5555:
{
pinattempt=3; correct=true;
String f=JOptionPane.showInputDialog("====================FBC====================\n\nFILIPINAS BANKING CORPORATION\n\t\t\tSELECT TYPE OF TRANSACTION\n\t\t[B] - BALANCE INQUIRY\n\t\t[W] - WITHDRAWAL\n\t\t[D] - DEPOSIT \n\t\t[C] - CANCEL");
char pick=f.charAt(0);
switch (pick)
{
case 'B':
case 'b': JOptionPane.showMessageDialog(null, "Account #: " +acct [4]+"\n"+"Account Name: " +name [4]+"\n"+"Balance: "+bal[4],"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE); break;
case 'W':
case 'w':
String q =JOptionPane.showInputDialog("Enter Amount to be WITHDRAWN: ");
wdamount = Integer.parseInt(q);
if ((wdamount<100)||(wdamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [4]+"\n"+"The current BALANCE is now: " +(bal[4]-wdamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE); break;
case 'D':
case 'd':
String l = JOptionPane.showInputDialog("Enter amount to be DEPOSITED: ");
depamount = Integer.parseInt(l);
if ((depamount<100)||(depamount%100!=0))
{
JOptionPane.showMessageDialog(null, "Invalid Amount!");System.exit(0);
}
JOptionPane.showMessageDialog(null,"Account Name: " +name [4]+"\n"+"The current BALANCE is now: " +(bal[4]+depamount),"Balance Inquiry",JOptionPane.INFORMATION_MESSAGE);
break;
}
}break;
default: //pin validation
{
pinattempt++; wrongall++; wrong = false;
if ((wrongall==3)&&(wrong==false))
{
JOptionPane.showMessageDialog(null,"CAPTURED CARD... PLEASE CALL 143-44"); System.exit(0);
}
} //pin validation up to here
}
}
}
}
}
}
}
答案 0 :(得分:0)
基于你在这里所说的基本思想是如何做到的(不是基于你的代码,因为它很多,只是一个例子)。将功能彼此分开并为它们创建不同的方法。在函数的最后,你可以简单地调用一个方法来再次显示主菜单。一个非常基本的例子可能是这样的。
public class Example {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
printMainMenue();
}
/**
* A basic method that should allways let you print the menue and input values
*/
static void printMainMenue() {
System.out.println("Option a");
System.out.println("Option b");
System.out.println("Option c");
System.out.println("Option d");
String input = scanner.nextLine();
if(input.equals("A")) {
optionA();
} else if(input.equals("B")) {
optionB();
} else if(input.equals("C")) {
optionC();
} else if(input.equals("D")) {
close();
} else {
System.out.println("Wrong input");
printMainMenue();
}
}
static void optionA () {
// Do stuff
System.out.println("You picked option A");
printMainMenue();
}
static void optionB () {
// Do stuff
System.out.println("You picked option B");
printMainMenue();
}
static void optionC () {
// Do stuff
System.out.println("You picked option C");
printMainMenue();
}
static void close() {
System.out.println("Goodbye");
}
}
对于您的代码,这意味着您将在不同方法中分离案例A,C,Q,S and so on
应该发生的事情。最后,您只需为这些方法提供printMenue
方法的回调,这将再次显示您的菜单。
如果你想要一个“更快,更脏的解决方案”,只需用循环while(true){...}
包围代码,并在用户选择退出程序时将其中断。