JAVA家庭收支记录

JAVA家庭收支记录,第1张

JAVA家庭收支记录
import java.util.Scanner;

public class familyaccount {
	

	public static void main(String[] args) {
		int balance=10000;
		boolean loop = true;
		String string = "收支t账户金额t收支金额t说  明n";
		do {
		System.out.println("---------------家庭收支记录-----------------");
		System.out.println("               1.收支明细 ");
		System.out.println("               2.登记收入 ");
		System.out.println("               3.登记支出 ");
		System.out.println("               4. 退出   ");
		System.out.println("               请选择<1-4>:");
		System.out.println("----------------------------------------");
		char sel=select();
		System.out.println();
		switch (sel){
			case '1': 
				System.out.println("--------------当前收支明细--------------");
				System.out.println(string);
				System.out.println("------------------------------------");
				break;
			case '2': 
				System.out.println("本次收入金额:");
				int number=readnumber();
				System.out.println("本次收入说明:");
				String str=str();
				balance+=number;
				string+="收入t"+balance+"t"+number+"t"+str+'n';
				System.out.println("---------------登记完成--------------");
				break;
			case '3': 
				System.out.println("本次支出金额:");
				int number1=readnumber();
				System.out.println("本次支出说明:");
				String str1=str();
				balance-=number1;
				string+="支出t"+balance+"t"+number1+"t"+str1+'n';
				System.out.println("---------------登记完成--------------");
				break;
			case '4': 
				System.out.println("是否确认退出(Y/N)?");
				char c =/confirm/i();
				if (c=='Y') {
					loop=false;	
				}
				break;
		default:
			readc();
			
		}
		}while(loop);
	}

	private static char select() {
		char c;
		c=keyboard(1).charAt(0);
		while(true) {
			if (c!='1'&&c!='2'&&c!='3'&&c!='4') {
				System.out.println("输入有误,请重新输入1-4之间的数字!");
			}
			else 
				break;
		}
		return c;
	}
	private static int readnumber() {
		int n;
		while(true) {
			String str=keyboard(4);
			try {
				n=Integer.parseInt(str);
				break;
			} catch (NumberFormatException e) {
				// TODO: handle exception
				System.out.println("数字输入有误,请重新输入!");
			}
		}
		return n;
	}
	private static String str() {
		String s=keyboard(8);
		return s;
	}
	private static char readc() {
		char c;
		c=keyboard(1).charAt(0);
		while (true) {
			if(c!='1'||c!='2'||c!='3'||c!='4') {
				System.out.println("输入有误,请重新输入!");
			}
			else 
				break;		
		}
		return c;
	}
	private static String keyboard(int len) {
		String str="";
		Scanner scanner=new Scanner(System.in);
		while(scanner.hasNext()) {
			str =scanner.nextLine();
			if (str.length()<1||str.length()>len) {
				System.out.println("输入有误,请重新输入!");
				continue;
			}
			break;
		}
		return str;
	}
	private static char /confirm/i() {
		char c = keyboard(1).charAt(0);
		while(true) {
			if (c=='Y'||c=='N') {
				break;
			}
			else {
				System.out.println("输入有误,请重新输入!");
			}
		}
		return c;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5080851.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存