Java收支记账小程序

收支记账小程序

此程序主要使用了循环、判断和相互的嵌套。在程序中利用循环来进行反复的记账,让收入和支出的数值都能再次从控制台中打印出来。

这个程序的难点就在于是否能利用好循环,如果利用好的话这个程序应该没有什么大的问题了。

程序的代码如下:

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

	}
}

运行结果

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值