Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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 - Fatal编程技术网

如何重定向回Java中的方法?

如何重定向回Java中的方法?,java,Java,我想知道如何重定向回java中的方法。出于测试目的,当我运行它时,它会询问我想做什么。我选择存款(出于测试目的),并有意输入低于250美元的金额。它返回我想要的提示,但程序刚刚结束。我想将程序重定向回存款方法的开头,以便程序继续运行。我知道我可以在存款部分的if,else if语句中再次写入存款方法,但我更希望程序重定向。这个节目只是为了好玩。我是编程新手,所以如果我给任何东西贴错标签,请很好地纠正我。这是完整的程序 import java.util.Scanner; class TestExc

我想知道如何重定向回java中的方法。出于测试目的,当我运行它时,它会询问我想做什么。我选择存款(出于测试目的),并有意输入低于250美元的金额。它返回我想要的提示,但程序刚刚结束。我想将程序重定向回存款方法的开头,以便程序继续运行。我知道我可以在存款部分的if,else if语句中再次写入存款方法,但我更希望程序重定向。这个节目只是为了好玩。我是编程新手,所以如果我给任何东西贴错标签,请很好地纠正我。这是完整的程序

import java.util.Scanner;
class TestException
{
  public static void main(String arg[])
  {
     Scanner awith = new Scanner(System.in);

     double bal = 0000.00;
     System.out.println("Deposit - D      Withdraw - W      Balance - B");       
     System.out.println("Would you like to deposit, withdraw, or check balance?");
     String a = awith.nextLine();
     if(a.equalsIgnoreCase("D"))
     {
         System.out.println("How much would you like to deposit?");
         double g = awith.nextDouble();

         if(g < 250.00)
         {
             System.out.println("Minimum of $250.00 must be deposited");
         }
         else if(g >= 250.00)
         {
         System.out.println("You deposited: $" + g);
         double newbal = bal + g;
         System.out.println("Your new balance is: $" + newbal);
         System.out.println("Thank you for using Folders Bank!");
        }
     }
     else if(a.equalsIgnoreCase("W"))
     {
         System.out.println("How much would you like to withdraw?");
         double x = awith.nextDouble();

         if(x > bal)
         {
             System.out.println("Invalid amount chosen for withdrawal!");
         }
         else if(x < 0.01)
         {
             System.out.println("Invalid amount chosen for withdrawal!");
         }
         else
         {
             System.out.println("You have successfully withdrew: $" + x);
             double newbal1 = bal - x;
             System.out.println("Your new balance is: $" + newbal1);
             System.out.println("Thank you for using Folders Bank!");
         }
     }
     else if(a.equalsIgnoreCase("B"))
     {   
        if(bal == 0.00)
        {
             System.out.println("You have an empty balance. Please deposit money.");
        }
        else if(bal >= 250.00)
        {
         System.out.println("Your balance is: $" + bal);
         System.out.println("Thank you for using Folders Bank!");
        }
     }
     else
     {
         System.out.println("Invalid entry! Thank you for using Folders Bank!");
     }
  }
}
import java.util.Scanner;
类测试异常
{
公共静态void main(字符串arg[])
{
扫描仪awith=新扫描仪(System.in);
双倍余额=0000.00;
System.out.println(“存款-D提取-W余额-B”);
System.out.println(“您想存款、取款还是检查余额?”);
字符串a=awith.nextLine();
如果(a.等效信号情况(“D”))
{
System.out.println(“您想存多少?”;
double g=awith.nextDouble();
如果(g<250.00)
{
System.out.println(“必须至少存入250.00美元”);
}
否则,如果(g>=250.00)
{
System.out.println(“您存入:$”+g);
双纽巴=bal+g;
System.out.println(“您的新余额为:$”+newbal);
System.out.println(“感谢您使用文件夹库!”);
}
}
否则,如果(a.等效信号情况(“W”))
{
System.out.println(“您想提取多少?”);
double x=awith.nextDouble();
如果(x>bal)
{
System.out.println(“为取款选择的金额无效!”);
}
否则如果(x<0.01)
{
System.out.println(“为取款选择的金额无效!”);
}
其他的
{
System.out.println(“您已成功提取:$”+x);
双纽巴利1=bal-x;
System.out.println(“您的新余额为:$”+newbal1);
System.out.println(“感谢您使用文件夹库!”);
}
}
否则,如果(a.等效信号情况(“B”))
{   
如果(bal==0.00)
{
System.out.println(“您的余额为空,请存款。”);
}
否则,如果(bal>=250.00)
{
System.out.println(“您的余额为:$”+bal);
System.out.println(“感谢您使用文件夹库!”);
}
}
其他的
{
System.out.println(“无效条目!感谢您使用文件夹库!”);
}
}
}
您可以执行以下操作: 来自09号线

System.out.println("Would you like to deposit, withdraw, or check balance and Exit(e)?");
String a = awith.next();
while(a.equalsIgnoreCase("e")!=true)
{
//yourcode
//all of your if conditions
System.out.println("Would you like to deposit, withdraw, or check balance and Exit(e)?");
a = awith.next();
}

如果您不想两次获得最后两行,您也可以使用do while。

虽然@Ajit的解决方案会起作用,但我建议使用
do。。while
而不仅仅是
while
,因为使用
while
必须重复提示和扫描-如下所示:

do {
    System.out.println("Would you like to deposit, withdraw, or check balance and Exit(e)?");
    a = awith.next();    
} while ( ! a.equalsIgnoreCase("e"));

围绕你的逻辑做一个循环。但是别忘了一个退出选项为什么我没有考虑一个循环?谢谢。使用while循环,确保循环的水晶是“只要你读到不同的东西”,并且你把你的退出条件