家庭收支记账软件
利用基础语法
无调用方法版
import java.util.Scanner;
public class HomeWork01{
private static int sum=10000;
private static String info="";
private static boolean flag=true;
public static void main(String[] args) {
int in = 0;
int out = 0;
String note="";
Scanner scanner = new Scanner(System.in);
do {
System.out.println("------------家庭收支记账软件-----------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记开支");
System.out.println(" 4.退 出");
System.out.println();
System.out.print(" 请输入1-4:");
int n = scanner.nextInt();
if (n == 1) {
System.out.println("近期开支如下:");
System.out.println(info);
System.out.println("余额:"+sum);
}
if (n == 2) {
System.out.println("本次收入金额:");
in = scanner.nextInt();
System.out.println("本次收入说明:");
note = scanner.next();
sum+=in;
info+="收入金额:"+in+"收入说明:"+note+"\n";
// InCome(in, note);
}
if (n == 3) {
System.out.println("本次支出金额:");
out=scanner.nextInt();
System.out.println("本次支出说明:");
note=scanner.next();
sum-=out;
info+="支出金额:"+out+"支出说明:"+note+"\n";
// pay(out, note);
}
if(n==4){
System.out.print("是否退出(Y/N):");
String t = scanner.next();
if (t.equals("Y")){
flag = false;
break;}
else if (t.equals("N"))
break;
else
System.out.print("输入错误,请重新输入:");
}
}while(flag);//if(n==4)&&t==Y结束循环
}
// public static void InCome(int in,String note){
// sum+=in;
// info+="收入金额:"+in+"收入说明:"+note+"\n";
// }
// public static void pay(int out,String note){
// sum-=out;
// info+="支出金额:"+out+"支出说明:"+note+"\n";
// }
}
调用同包方法版
import java.awt.*;
import java.util.Scanner;
public class HomeWork01 {
protected static String info;
protected static int sum=10000;
protected static boolean flag=true;
public static void main(String[] args) {
int in = 0;
int out = 0;
String note = "";
Scanner scanner = new Scanner(System.in);
do {
Use.Menu();
int n = scanner.nextInt();
if (n == 1) {
System.out.println("近期开支如下:");
System.out.println(info);
System.out.println("余额:" + sum);
}
if (n == 2) {
System.out.println("本次收入金额:");
in = scanner.nextInt();
System.out.println("本次收入说明:");
note = scanner.next();
Use.InCome(in, note);
}
if (n == 3) {
System.out.println("本次支出金额:");
out = scanner.nextInt();
System.out.println("本次支出说明:");
note = scanner.next();
Use.pay(out, note);
}
if (n == 4) {
System.out.print("是否退出(Y/N):");
String t = scanner.next();
Use.Exit(t);
}
} while (flag);//if(n==4)&&t==Y结束循环
}
}
import java.util.Scanner;
public class Use {
// private static int sum=10000;
// private static String info="";
// private static boolean flag=true;
Scanner scanner = new Scanner(System.in);
public static void Menu() {//调用主菜单
System.out.println("------------家庭收支记账软件-----------");
System.out.println(" 1.收支明细");
System.out.println(" 2.登记收入");
System.out.println(" 3.登记开支");
System.out.println(" 4.退 出");
System.out.println();
System.out.print(" 请输入1-4:");
}
public static void Sum() {//余额及开销总数据
System.out.println("近期开支如下:");
System.out.println(HomeWork01.info);
System.out.println("余额:"+HomeWork01.sum);
}
public static void InCome(int in,String note){
HomeWork01.sum+=in;
HomeWork01.info+="收入金额:"+in+"收入说明:"+note+"\n";
}
public static void pay(int out,String note){
HomeWork01.sum-=out;
HomeWork01.info+="支出金额:"+out+"支出说明:"+note+"\n";
}
public static void Exit(String t) {
if (t.equals("Y")){
HomeWork01.flag = false;
}
else
System.out.print("输入错误,请重新输入:");
}
}