public static void main(String[] args) {
Scanner keybNum = new Scanner(System.in);
Scanner keybStr = new Scanner(System.in);
boolean yn = false;
//Start of program
System.out.println("Welcome to The Currency Exchange Converter");
System.out.print("Are you an Account Holder (y or n)? ");
String AccHolder = keybStr.next();
boolean blnYN = true;
//validation of y/n answer
while (blnYN) {
if (AccHolder.equalsIgnoreCase("y")) {
yn = true;
blnYN = false;
break;
}//if
else if (AccHolder.equalsIgnoreCase("n")) {
yn = false;
blnYN = false;
break;
}//else if
else {
System.out.println("Invalid value entered. Choose either y/n.");
AccHolder = keybStr.next();
}//else
}//while
//Start of menu choices
System.out.println("Please choose from the following menu.");
System.out.println("\n1: Exchange another currency for Sterling");
System.out.println("2: Buy another currency from us");
System.out.println("0: Exit");
System.out.print("Which option? ");
int MenuChoice = keybNum.nextInt();
//Exchange Variables (First option)
double Euro = 1.37;
double USAD = 1.81;
double JapYen = 190.00;
double Zloty = 5.88;
//Buy Variables (Second Option)
double EuroB = 1.21;
double USADB = 1.61;
double JapYenB = 163.00;
double ZlotyB = 4.89;
//First menu choice screen
if (MenuChoice == 1) {
System.out.println("Which of the following currencies do you wish to exchange into sterling?");
System.out.println("Euro - EUR");
System.out.println("USA Dollar - USD");
System.out.println("Japanese Yen - JPY");
System.out.println("Polish Zloty - PLN");
System.out.print("Please enter the three letter currency: ");
//Currency input validation
String CurChoice = "";
boolean isCorrectCurrency = false;
do {
CurChoice = keybStr.next();
isCorrectCurrency = CurChoice.matches("^EUR|USD|JPY|PLN$");
if (isCorrectCurrency) {
System.out.println("");
} else {
System.out.print("Invalid Currency Entered. Please Retry: ");
}
} while (!isCorrectCurrency);
//Exchange amount calculator
System.out.print("Enter the amount you wish to exchange of " + CurChoice + ": ");
double ExcAmount = keybStr.nextInt();
double result = 0.00;
//Selection and calculation of user's input
if (CurChoice.equals("EUR")) {
result = ExcAmount / Euro;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
} else if (CurChoice.equals("USD")) {
result = ExcAmount / USAD;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
} else if (CurChoice.equals("JPY")) {
result = ExcAmount / JapYen;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
} else if (CurChoice.equals("PLN")) {
result = ExcAmount / Zloty;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
} else {
System.out.println("");
}
DecimalFormat df = new DecimalFormat("#.##");
double commission = 0;
if (ExcAmount < 1000) {
commission = result * 0.02;
} else {
commission = result * 0.01;
}
String stringToPrint = "";
if (!yn) {
stringToPrint = "Commission\t=£" + df.format(commission);
} else {
stringToPrint = "Commission \t= Not charged";
}
System.out.println(stringToPrint);
double netPayment = result - commission;
System.out.println("Total\t\t=£" + df.format(netPayment));
}//if
//Start of second option
else if (MenuChoice == 2) {
System.out.println("Which of the following currencies do you wish to buy from us?");
System.out.println("Euro - EUR");
System.out.println("USA Dollar - USD");
System.out.println("Japanese Yen - JPY");
System.out.println("Polish Zloty - PLN");
System.out.print("Please enter the three letter currency: ");
//Currency input validation
String CurChoice = "";
boolean isCorrectCurrency = false;
do {
CurChoice = keybStr.next();
isCorrectCurrency = CurChoice.matches("^EUR|USD|JPY|PLN$");
if (isCorrectCurrency) {
System.out.println("");
} else {
System.out.print("Invalid Currency Entered. Please Retry: ");
}
} while (!isCorrectCurrency);
System.out.print("Enter the amount you wish to buy in £ of " + CurChoice + ": £");
double BuyAmount = keybStr.nextInt();
double result = 0.00;
//Selection and calculation of user's input
if (CurChoice.equals("EUR")) {
result = BuyAmount * EuroB;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("£" + BuyAmount + "\t\t= " + df.format(result) + " " + CurChoice);
} else if (CurChoice.equals("USD")) {
result = BuyAmount * USADB;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("£" + BuyAmount + "\t\t= " + df.format(result) + " " + CurChoice);
} else if (CurChoice.equals("JPY")) {
result = BuyAmount * JapYenB;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("£" + BuyAmount + "\t\t= " + df.format(result) + " " + CurChoice);
} else if (CurChoice.equals("PLN")) {
result = BuyAmount * ZlotyB;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("£" + BuyAmount + "\t\t= " + df.format(result) + " " + CurChoice);
} else {
System.out.println("");
}
DecimalFormat df = new DecimalFormat("#.##");
double commission = 0;
if (BuyAmount < 1000) {
commission = result * 0.02;
} else if (BuyAmount >= 1000) {
commission = result * 0.01;
} else {
}
String stringToPrint = "";
if (!yn) {
stringToPrint = "Commission\t= " + df.format(commission) + " " + CurChoice;
} else {
stringToPrint = "Commission \t= Not charged";
}
System.out.println(stringToPrint);
double netPayment = result - commission;
System.out.println("Total\t\t= " + df.format(netPayment) + " " + CurChoice);
}//else if
//Action if the user selects 0 to close the system.
else {
System.out.println("Thank you for visiting.");
}//else
}//main
所以我不确定在我的程序中使用的最佳循环。如果用户在程序开始时输入1或2,我希望程序重复。该程序是一个简单的货币兑换器。有什么帮助吗?
答案 0 :(得分:2)
理想情况下,您有一个符合
的程序逻辑int choice = 0;
while( (choice = readChoice()) != 0 ){
// process according to choice
// ...
}
以静态方法包装第一个菜单
pubic static int readChoice()
并添加while语句。
(您的所有代码都将受益于方法的结构化。当您添加新功能时,它会变得笨拙。请记住 - 我看过旧版本;-))
答案 1 :(得分:0)
考虑一个do { } while(...);
循环。它应该是do {...} while (MenuChoice == 1 || MenuChoice == 2);
确保在到达MenuChoice
语句之前始终为while
指定一个值。 do while
应该从行//Start of menu choices
开始到底部。
另外,请考虑MenuChoice