哪位大神知道这道java题的代码该怎么写?
发布网友
发布时间:2023-08-13 03:11
我来回答
共5个回答
热心网友
时间:2023-08-14 04:18
public static void showBalance(double balance) {
System.out.println();
System.out.println("*** 当前余额为:" + balance + "元 ***");
System.out.println();
}
public static void bank(double balance) {
Scanner scanner = null;
try {
boolean exit = false;
while (!exit) {
scanner = new Scanner(System.in);
System.out.println("1.存款\t2.取款\t0.退出");
System.out.print("请选择您需要办理的业务:");
int option = scanner.nextInt();
switch (option) {
case 0:
System.out.println("谢谢使用!");
exit = true;
break;
case 1:
System.out.print("请输入存款金额:");
int put = scanner.nextInt();
if (put <= 0) {
System.out.print("您的输入有误,请重新输入:");
put = scanner.nextInt();
}
balance = balance + put;
System.out.println("存款成功!");
showBalance(balance);
break;
case 2:
System.out.print("请输入取款金额:");
int get = scanner.nextInt();
if (get <= 0) {
System.out.print("您的输入有误,请重新输入:");
get = scanner.nextInt();
}
if (balance - get < 0) {
System.out.print("您的余额不足,请重新输入:");
get = scanner.nextInt();
}
balance = balance - get;
System.out.println("取款成功");
showBalance(balance);
break;
default:
System.out.println("您的输入有误,请重新输入:");
option = scanner.nextInt();
break;
}
}
} finally {
scanner.close();
}
}
public static void main(String[] args) {
bank(30000);
}
热心网友
时间:2023-08-14 04:18
import java.util.Scanner;
public class ATM {
private float balance = 0;
public ATM() {
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
public void mainMenu(){
System.out.println("1 存款 2 取款 0 退出");
System.out.print("请选择你需要办理的业务:");
}
public void continueMenu(){
System.out.println("成功");
System.out.println("\n***当前余额为:"+this.balance+"元***\n");
}
public boolean withDraw(Scanner scanner){
System.out.print("请输入取款金额:");
float money = scanner.nextFloat();
if (money < 0){
System.out.println("取款金额错误");
return false;
}
if (money > getBalance()){
System.out.println("余额不足");
return false;
}
setBalance(getBalance() - money);
System.out.print("取款");
return true;
}
public boolean deposit(Scanner scanner){
System.out.print("请输入存款金额:");
float money = scanner.nextFloat();
if (money < 0){
System.out.println("存款金额错误");
return false;
}
setBalance(getBalance() + money);
System.out.print("存款");
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ATM atm = new ATM();
boolean flag = false;
int menuNum = 0;
while (true){
atm.mainMenu();
menuNum = scanner.nextInt();
if(menuNum==0){
break;
}
switch (menuNum){
case 1:{
flag = atm.deposit(scanner);
break;
}
case 2:{
flag = atm.withDraw(scanner);
break;
}
default:{
break;
}
}
if(flag) atm.continueMenu();
}
System.out.println("谢谢使用!");
scanner.close();
}
}
热心网友
时间:2023-08-14 04:19
我的颈椎病神奇的好了~
热心网友
时间:2023-08-14 04:19
啊!!!我的眼睛!!!
热心网友
时间:2023-08-14 04:20
看不清图片