java银行存款取款
时间: 2024-01-12 07:22:42 浏览: 149
以下是一个简化版的Java银行存款取款系统的代码示例:
```java
import java.util.Scanner;
public class BankSystem {
private static double balance = 0; // 初始余额为0
public static void main(String[] args) {
login(); // 登录验证
menu(); // 显示菜单
}
// 登录验证
public static void login() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
// 这里可以添加账户密码验证的逻辑
}
// 显示菜单
public static void menu() {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("请选择操作:");
System.out.println("1. 查看余额");
System.out.println("2. 存款");
System.out.println("3. 取款");
System.out.println("4. 退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
checkBalance();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
System.out.println("谢谢使用!");
break;
default:
System.out.println("无效的选择,请重新输入!");
break;
}
} while (choice ! 4);
}
// 查看余额
public static void checkBalance() {
System.out.println("当前余额为:" + balance);
}
// 存款
public static void deposit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
balance += amount;
System.out.println("存款成功!");
}
// 取款
public static void withdraw() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
if (amount > balance) {
System.out.println("余额不足!");
} else {
balance -= amount;
System.out.println("取款成功!");
}
}
}
```
阅读全文