java - 如何返回程序中的步骤?

标签 java if-statement

我有一个银行帐户程序,我正在重写我在学校编写的程序,我想知道如何返回程序中的某个步骤。

那么,在我创建帐户并选择提款选项后,我想返回并再次收到选项提示,该怎么办? (见代码中的注释) 非常感谢..

主类:

import java.text.*;

public class BankAccountTest {
 public static void main (String args[]){

            NumberFormat formatter = NumberFormat.getNumberInstance();
            formatter.setMaximumFractionDigits(2);  // Helps formatter format for final output
            formatter.setMinimumFractionDigits(2);
            ConsoleReader console = new ConsoleReader(System.in);

     System.out.println("Hello, would you like to make a new bank account?");
     String newA = console.readLine();

     if(newA.equalsIgnoreCase("yes")){
         System.out.println("How much would you like to deposit initially?");
         double init = console.readDouble();

         BankAccount account = new BankAccount(init);

         System.out.println("Your account is created, what would you like to do? /n 1: Get Balance /n 2: Get Account ID /n 3: Make a Withdrawl /n 4: Make a Deposit?");
         String option = console.readLine();

         if(option.equalsIgnoreCase("get balance")){
             System.out.println(account.getBalance());  //go back to the if after this excecutes
         }
     }

 }
}

银行账户类别:

public class BankAccount {

    public static int bankID = 0;

    //constructor called by BankAccount michaelsBank = new BankAccount();
    public BankAccount(){
        balance = 0;
        accnum = bankID++;
    }

    //Constructs a bank account with an initial deposit, will be used if given a number for a parameter
    public BankAccount(double initialBalance){
        balance = initialBalance;
    }


        public void deposit(double amount){
            balance = balance + amount;
        }

        public void withdraw(double amount){
            balance = balance - amount;
        }

        public double getBalance(){
            return balance;
        }

        public int getID(){
            return accnum;
        }

        private int accnum;
        private double balance;

}

控制台阅读器类:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

/** 
   A class to read strings and numbers from an input stream.
   This class is suitable for beginning Java programmers.
   It constructs the necessary buffered reader, 
   handles I/O exceptions, and converts strings to numbers.
*/

public class ConsoleReader
{  /**
      Constructs a console reader from an input stream
      such as System.in
      @param inStream an input stream 
   */
   public ConsoleReader(InputStream inStream)
   {  reader = new BufferedReader
         (new InputStreamReader(inStream)); 
   }

   /**
      Reads a line of input and converts it into an integer.
      The input line must contain nothing but an integer.
      Not even added white space is allowed.
      @return the integer that the user typed
   */
   public int readInt() 
   {  String inputString = readLine();
      int n = Integer.parseInt(inputString);
      return n;
   }

   /**
      Reads a line of input and converts it into a floating-
      point number. The input line must contain nothing but 
      a nunber. Not even added white space is allowed.
      @return the number that the user typed
   */
   public double readDouble() 
   {  String inputString = readLine();
      double x = Double.parseDouble(inputString);
      return x;
   }

   /**
      Reads a line of input. In the (unlikely) event
      of an IOException, the program terminates. 
      @return the line of input that the user typed, null
      at the end of input
   */
   public String readLine() 
   {  String inputLine = "";

      try
      {  inputLine = reader.readLine();
      }
      catch(IOException e)
      {  System.out.println(e);
         System.exit(1);
      }

      return inputLine;
   }

   private BufferedReader reader; 
}

最佳答案

 String option = console.readLine();

         while(option.equalsIgnoreCase("get balance")){
             System.out.println(account.getBalance());  //go back to the if after this excecutes
             option =  console.readLine();

         }

简单的循环应该可以解决这个问题。 另请注意,您应该在该行执行后再次读取控制台,以防止无限循环。

关于java - 如何返回程序中的步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251720/

相关文章:

java - 使用 J2s [Auto complete ComboBox] 的 JTable 自动完成不工作

java - 如何将 BMP 图像转换为 8 位灰度图像

mysql - If...ELSE 在 MySQL 中使用子查询

tsql - T-SQL 中的单行 IF

php - 从具有共享 ID 的相应数据库中有多个条目的数据库中获取条目(准备好的语句)

java - 如何在构造函数类中创建数组?

java - 使用java搜索JSon字符串

java - 计算沿相对对角线的点偏移

c++ - 在 C 和 C++ 编程中使用哪个更好?

python - 当条件为非 bool 值时,如何在列表理解中使用 "else"?