文章目录
- 简介
- 代码实现
简介
该项目主要用来练习,Java的变量,运算符,分支结构和循环结构的知识点。
程序界面如下:
登记收入
登记支出
收支明细
程序退出
代码实现
package project;
import java.util.Scanner;
import static java.lang.System.exit;
/**
* ClassName: Account
* Package: project
* Description: 家庭收支记账软件(简易版)
*
* @Author: Ethan
* @Create 2023/10/24 20:50
* @Version 1.0
*/
public class Account {
public static void main(String[] args) {
home();
Scanner scanner = new Scanner(System.in);
int btn = scanner.nextInt();
String detail = "";
int balance = 0;
while( true ) {
switch (btn) {
case 1:
if(detail.equals("")) {
System.out.println("暂无记录!");
} else {
System.out.println("-------------------当前收支明细记录-------------------");
System.out.println("收支\t账户金额\t收支金额\t说明");
System.out.println(detail);
}
break;
case 2:
System.out.println("收入金额:");
int in = scanner.nextInt();
balance += in;
System.out.println("收入说明:");
String inExplain = scanner.next();
detail += ("收入\t" + balance + "\t" + in + "\t" + inExplain + "\n");
break;
case 3:
System.out.println("支出金额:");
int out = scanner.nextInt();
if( balance >= out){
balance -= out;
System.out.println("支出说明:");
String outExplain = scanner.next();
detail += ("支出\t" + balance + "\t" + out + "\t" + outExplain + "\n");
} else {
System.out.println("余额不足!");
}
break;
case 4:
System.out.println("确定退出吗?(Y/N)");
char confirm = scanner.next().toLowerCase().charAt(0);
if(confirm == 'y') {
System.out.println("系统退出");
exit(0);
}
default:
System.out.println("输入有误!");
}
home();
btn = scanner.nextInt();
}
}
/**
* @ description: 用来打印程序主界面
* @ return: void无返回值
*/
public static void home() {
System.out.println("-------------------家庭收支记账软件-------------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退出");
System.out.println(" 请选择(1~4):");
}
}