繁体   English   中英

不确定要使用的最佳循环

[英]Unsure about the best loop to use

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,我希望程序重复。 该程序是一个简单的货币兑换转换器。 有什么帮助吗?

理想情况下,你有一个程序逻辑,根据:

int choice = 0;
while( (choice = readChoice()) != 0 ){
    // process according to choice
    // ...
}

用静态方法包装第一个菜单:

public static int readChoice()

并添加while语句。

(您的所有代码都将从方法结构中受益。随着您添加新功能,它变得笨拙。记住 - 我看过旧版本)

考虑一个do { } while(...); 环形。 应该do {...} while (MenuChoice == 1 || MenuChoice == 2); 确保在到达while语句之前始终为MenuChoice指定一个值。 do while应该从行//Start of menu choices到底部。

另外,请考虑MenuChoice的开关盒

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM