循环程序回到''菜单''

时间:2015-10-31 15:13:29

标签: java loops

我只是在编写这个程序,这是为了在星期一训练自己进行这项升级考试。

我想补充的一点是:在用户完成其中一个交换选项1 / 2 / 3后,我想提供让用户返回到开始welcome to the money exchange! etc....

我尝试了一些for循环和一个while循环,但我无法让它工作。 如果在货币兑换流程之后用户通过键入yn可以选择返回到开头,那会很酷吗?

/* This program is written as a excercise to prep myself for exams.  * In this program the user can:  * 1. Select a currency (other than euro's)  * 2. Input the amount of money * 3. transfer the amount of currency to euro's  */

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);




    System.out.println(" Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n  ");
    System.out.print("Input : ");

    DecimalFormat df = new DecimalFormat() ;
    df.setMaximumFractionDigits(2);

    int choice = input.nextInt() ;
    double transfee = 2.41 ;
    double USrate = 0.9083 ;
    double GBrate = 1.4015 ;
    double YENrate = 0.0075 ;

    if (choice > 3 || choice < 1) {
        System.out.println("Invalid input!...... Please try agian\n");
    } else {
        if(choice == 1)  {
            System.out.println("You have choosen for US dollar \n");
            System.out.print("Please enter amount US dollar: ");
            double USamount = input.nextDouble() ;
            double deuros = USamount * USrate ;
            double ddisburse = deuros - transfee ;

            System.out.print("\nInput amount US dollar:. " + USamount + "\n");
            System.out.print("Worth in euro's:........ " + df.format(deuros) + "\n");    
            System.out.print("Transfer cost:.......... " + transfee + "\n");
            System.out.print("Amount to disburse:..... " + df.format(ddisburse) + "\n" );


        }else {
            if(choice == 2){
                System.out.println("You have choosen for GB pounds");
                System.out.print("Please enter amount GB ponds: ");
                double GBamount = input.nextDouble();
                double geuros = GBamount * GBrate ;
                double gdisburse = geuros - transfee;

                System.out.print("\nInput amount GB pound:. " + GBamount + "\n");
                System.out.print("Worth in euro's........ " + df.format(geuros) + "\n");
                System.out.print("Transfer cost:......... " + transfee + "\n");
                System.out.print("Amount to disburse:.... " + df.format(gdisburse) + "\n");


            }else {
                if(choice == 3){
                    System.out.println("You have choosen for Yen");
                    System.out.print("Please enter amount Yen: ");
                    double  YENamount = input.nextDouble();
                    double yeuros = YENamount * YENrate ;
                    double ydisburse = yeuros - transfee ;

                    System.out.print("\nInput amount Yen:... " + YENamount + "\n");
                    System.out.print("Worth in euro's..... " + df.format(yeuros) + "\n");
                    System.out.print("Transfer cost:...... " + transfee + "\n");
                    System.out.print("Amount to disburse:. " + df.format(ydisburse) + "\n");



            }
            }
        }
    }






    }


}

2 个答案:

答案 0 :(得分:1)

你可以使用while循环包装你的程序,它会检查用户是否在最后输入'y':

import java.text.DecimalFormat;
import java.util.Scanner;

class YourClassName
{
    public static void main(String[] args)
    {
        boolean askAgain = true;

        while (askAgain)
        {
            Scanner input = new Scanner(System.in);

            System.out.println(
                    " Welcome to the money exchange! \n Please pick one of the currencies by useing 1 / 2 / 3 \n \n 1 = US dollar \n 2 = GB pounds \n 3 = Yen \n  ");
            System.out.print("Input : ");

            DecimalFormat df = new DecimalFormat();
            df.setMaximumFractionDigits(2);

            int choice = input.nextInt();
            double transfee = 2.41;
            double USrate = 0.9083;
            double GBrate = 1.4015;
            double YENrate = 0.0075;

            if (choice > 3 || choice < 1)
            {
                System.out.println("Invalid input!...... Please try agian\n");
            } else
            {
                if (choice == 1)
                {
                    System.out.println("You have choosen for US dollar \n");
                    System.out.print("Please enter amount US dollar: ");
                    double USamount = input.nextDouble();
                    double deuros = USamount * USrate;
                    double ddisburse = deuros - transfee;

                    System.out.print(
                            "\nInput amount US dollar:. " + USamount + "\n");
                    System.out.print("Worth in euro's:........ "
                            + df.format(deuros) + "\n");
                    System.out.print(
                            "Transfer cost:.......... " + transfee + "\n");
                    System.out.print("Amount to disburse:..... "
                            + df.format(ddisburse) + "\n");

                } else
                {
                    if (choice == 2)
                    {
                        System.out.println("You have choosen for GB pounds");
                        System.out.print("Please enter amount GB ponds: ");
                        double GBamount = input.nextDouble();
                        double geuros = GBamount * GBrate;
                        double gdisburse = geuros - transfee;

                        System.out.print(
                                "\nInput amount GB pound:. " + GBamount + "\n");
                        System.out.print("Worth in euro's........ "
                                + df.format(geuros) + "\n");
                        System.out.print(
                                "Transfer cost:......... " + transfee + "\n");
                        System.out.print("Amount to disburse:.... "
                                + df.format(gdisburse) + "\n");

                    } else
                    {
                        if (choice == 3)
                        {
                            System.out.println("You have choosen for Yen");
                            System.out.print("Please enter amount Yen: ");
                            double YENamount = input.nextDouble();
                            double yeuros = YENamount * YENrate;
                            double ydisburse = yeuros - transfee;

                            System.out.print("\nInput amount Yen:... "
                                    + YENamount + "\n");
                            System.out.print("Worth in euro's..... "
                                    + df.format(yeuros) + "\n");
                            System.out.print(
                                    "Transfer cost:...... " + transfee + "\n");
                            System.out.print("Amount to disburse:. "
                                    + df.format(ydisburse) + "\n");

                        }
                    }
                }
            }

            System.out.println("Do you want to do another calculation? (y/n)");
            String againAnswer = input.next();
            askAgain = againAnswer.equalsIgnoreCase("y");
        }
    }
}

首先将布尔变量设置为true,可以进入循环。只要他在最后键入y,系统就会询问用户。每个其他角色都会退出循环:

String againAnswer = input.next();
askAgain = againAnswer.equalsIgnoreCase("y");

您还可以检查明确的n,但这取决于您。

答案 1 :(得分:1)

将代码置于while循环(while(true))内。在每个if块的末尾 添加一个嵌套的if。

System.out.print(Do you want to continue?");
if(in.next().equals("Y")) {
continue;
}

你已经为退出添加了一个额外的菜单(第4个):

if(choice == 4){
break;
}