收支记账小程序
此程序主要使用了循环、判断和相互的嵌套。在程序中利用循环来进行反复的记账,让收入和支出的数值都能再次从控制台中打印出来。
这个程序的难点就在于是否能利用好循环,如果利用好的话这个程序应该没有什么大的问题了。
程序的代码如下:
import java.util.Scanner;
public class Bookkeeping {
public static void main(String[] sarg) {
int option, confirm; //定义变量
double initial = 10000,income, expenditure;
String explain, RevenueAndExpenditure, detailed = "";
boolean judge = true; //让记账进行循环
while(judge){
System.out.println("--------------------收支记账小程序--------------------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记支出");
System.out.println(" 4.退 出");
System.out.print(" 请选择<1-4>:");
Scanner scan = new Scanner(System.in);
option = scan.nextInt(); //获取用户的输入数字,来进行选择。
if(option >=1 && option <=4) {//若在刚开始的界面输入的数字小于1且大于4就会让用户重新输入数字
switch(option) {
case 1 : System.out.println("--------------------当前收支明细纪录---------------------");
System.out.println("收支 " + "账户金额 " + "收支金额 " + "说明");
System.out.println(detailed);//打印出用户输入的数值和收支的方式
break;
case 2 : System.out.print("本次收入金额:");
income = scan.nextDouble();
System.out.println();
System.out.print("收入说明:");
explain = scan.next();
initial += income;
RevenueAndExpenditure = "收入";
detailed += (RevenueAndExpenditure + " " + initial + " " + income + " " + explain + "\n");//记录用户输入的数值
break;
case 3 : System.out.print("本次支出的金额:");
expenditure = scan.nextDouble();
if(expenditure > initial && initial > 0)//若支出的金额大于账户就会重新输入
{
System.out.println("支出金额大于账户金额无法统计,请重新输入:");
expenditure = scan.nextDouble();
}
else if(initial <= 0)//若账户的金额小于零就无法在进行统计
{
System.out.println("您的账户余额已不足,无法再进行统计。");
break;
}
//else
System.out.print("支出说明:");
explain = scan.next();
RevenueAndExpenditure = "支出";
initial -= expenditure;
detailed += (RevenueAndExpenditure + " " + initial + " " + expenditure + " " + explain + "\n");
break;
case 4 :System.out.print("请问您要退出吗?[1/0]:");
confirm = scan.nextInt();
if(confirm == 1)
judge = false;
}
}
else
System.out.println("输入错误,请重新输入!\n");
}
}
}