Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将程序循环回'';菜单'';_Java_Loops - Fatal编程技术网

Java 将程序循环回'';菜单'';

Java 将程序循环回'';菜单'';,java,loops,Java,Loops,我刚刚写了这个程序,它是为了训练我自己,为这个星期一即将到来的考试做准备 我想补充的一点是:在用户使用其中一个兑换选项1/2/3后,我想给用户一个选项,让用户回到开始欢迎使用货币兑换!等等… 我试过一些for循环和while循环,但都没能成功。 如果在货币兑换过程之后,用户可以通过键入y或n返回到开头,这会很酷吗 /*本程序是为准备考试而编写的练习。 *在此程序中,用户可以: *1。选择一种货币(欧元除外) *2。输入金额 *3。将货币金额转移到欧元 */ public static void

我刚刚写了这个程序,它是为了训练我自己,为这个星期一即将到来的考试做准备

我想补充的一点是:在用户使用其中一个兑换选项
1
/
2
/
3
后,我想给用户一个选项,让用户回到开始
欢迎使用货币兑换!等等…

我试过一些for循环和while循环,但都没能成功。 如果在货币兑换过程之后,用户可以通过键入
y
n
返回到开头,这会很酷吗

/*本程序是为准备考试而编写的练习。
*在此程序中,用户可以:
*1。选择一种货币(欧元除外)
*2。输入金额
*3。将货币金额转移到欧元
*/

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");



            }
            }
        }
    }






    }


}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“欢迎使用货币兑换!\n请使用1/2/3\n\n 1=美元\n 2=GB磅\n 3=日元\n”选择一种货币);
系统输出打印(“输入:”);
DecimalFormat df=新的DecimalFormat();
df.setMaximumFractionDigits(2);
int choice=input.nextInt();
双transfee=2.41;
双美率=0.9083;
双GBrate=1.4015;
双倍年利率=0.0075;
如果(选项>3 | |选项<1){
System.out.println(“无效输入!……请再试一次\n”);
}否则{
如果(选项==1){
System.out.println(“您已经为美元选择了\n”);
系统输出打印(“请输入美元金额:”);
double USamount=input.nextDouble();
双deuros=美国蒙特*美国费率;
双ddisburse=双数-transfee;
系统输出打印(“\n输入金额美元:.”+USamount+“\n”);
系统输出打印(“欧元价值:………”+df.格式(deuros)+“\n”);
系统输出打印(“传输成本:……..”+传输对象+“\n”);
系统输出打印(“支付金额:…..”+df.format(ddisburse)+“\n”);
}否则{
如果(选项==2){
System.out.println(“您已选择GB磅”);
系统输出打印(“请输入金额:”);
double GBamount=input.nextDouble();
双倍geuros=GBmount*GBrate;
双gdisburse=geuros-transfee;
系统输出打印(“\n输入金额GB磅:.”+GBamount+“\n”);
System.out.print(“欧元价值……”+df.format(geuros)+“\n”);
系统输出打印(“传输成本:……”+传输对象+“\n”);
系统输出打印(“支付金额:…..”+df.format(gdisburse)+“\n”);
}否则{
如果(选项==3){
System.out.println(“您已选择日元”);
系统输出打印(“请输入金额:”);
double YENamount=input.nextDouble();
双yeuros=YENamount*YENrate;
双ydisburse=yeuros-transfee;
系统输出打印(“\n输入金额日元:…”+YENamount+“\n”);
System.out.print(“欧元价值…”+df.format(yeuros)+“\n”);
系统输出打印(“传输成本:……”+传输对象+“\n”);
系统输出打印(“支付金额:.”+df.format(ydisburse)+“\n”);
}
}
}
}
}
}

您可以使用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");
        }
    }
}

您也可以检查显式
n
,但这取决于您。

将代码放入while循环(
while(true)
)。在每个
if
块的末尾 如果需要,请添加一个嵌套

System.out.print(Do you want to continue?");
if(in.next().equals("Y")) {
continue;
}
您还为退出添加了一个额外菜单(第4个):

if(choice == 4){
break;
}

..}else{if(choice==2){
似乎有点奇怪。为什么不干脆
..}else if(choice==2){
?或者使用
开关
-语句。基本上,我想给用户返回开始的选项,这样选择货币的选项就会回来,用户可以重新运行程序,
..}否则如果(choice==2){
..}做同样的事{if(choice==2){
?谢谢你的提示!仍在学习java的基础知识:)您还可以通过使用
else if
来简化这一点,这将允许您取消检测这些条件。您还可以使用
switch
语句。感谢您的回答!,,我也查看了while语句,但我不知道如何应用循环。这是一个很好的示例!我有一个问题,while语句是什么布尔值是否用于循环?只要布尔值的计算结果为
true
,循环就会(再次)运行。这就是为什么带有
while(true)
的循环会永远运行(或者直到以
return
break
结束)。在while循环结束时,布尔值被重新分配,因此只有当输入为
y
y
时,它才是真的。非常感谢!我要将此应用到程序:)因此基本上我需要在每个
if(){}中添加注释中的第一个代码块
statement?以及代码末尾的第二个块,用于向用户提供停止程序循环的选项?这是正确的。一个更正。因为您已经声明了一个
Scanner
对象变量,所以可以使用它。我已经删除了该声明
if(choice == 4){
break;
}