Java realizes account withdrawal and deposit operations

This article mainly introduces the Java implementation of account withdrawal and deposit operation. The example code is introduced in great detail, which has a certain reference value for everyone's study or work. Friends in need can refer to it

Java write a program to complete the operation of withdrawal and deposit from an account

(1) The entered deposit amount is. If it is non numeric, capture one and process it

(2) The operation account class is. If the withdrawal amount is greater than the balance, exception handling will be performed

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;
      }
    }

  }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>