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

时间:2014-07-28 10:06:18

标签: java

我想知道如何重定向回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!");
     }
  }
}

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作: 从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,如果你不想两次获得最后两行。

答案 1 :(得分:0)

虽然@ 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"));