溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用JAVA實現賬戶取款和存款操作

發(fā)布時間:2021-04-14 14:02:51 來源:億速云 閱讀:379 作者:小新 欄目:編程語言

這篇文章主要介紹如何使用JAVA實現賬戶取款和存款操作,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

JAVA 編寫一個程序完成從某賬戶取款和存款的操作

(1)輸入存款金額是,如果非數值型,捕獲一場并進行處理

(2)操作賬戶類是,如果取款金額大于余額時則做異常處理

import java.util.Scanner;

public class Blank {
  public static void main(String[] args) {
    float residue = 10000.0f;
    String type;
    Scanner scn = new Scanner(System.in);
    while(true) {
      System.out.println("****當前賬戶余額:"+residue+"****");
      System.out.println("1.存錢 2.取錢 0.退出");
      System.out.print("請選擇(1,2,0):");
      type = scn.nextLine();
      if(type.equals("1")) {
        System.out.print("請輸入金額:");
        try {
          int money = scn.nextInt();
          residue = money+residue;
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println("輸入錯誤...");
          scn.nextLine();
        }
      }
      else if(type.equals("2")) {
        try {
          System.out.print("請輸入:");
          int money = scn.nextInt();
          if(money>residue) {
            throw new Exception();
          } else {
            residue = residue-money;
          }
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println("余額不足");
          scn.nextLine();
        }
      }
      
      else if(type.equals("0")) {
        System.out.println("bye..");
        break;
      }
    }
    
  }
}

以上是“如何使用JAVA實現賬戶取款和存款操作”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI