目标
面向过程版
代码
package new_pluse;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;public class change_common{public static void main(String arg[]){//定义相关变量Scanner scanner = new Scanner(System.in);String key="";String detaile="1 ==============零钱通明细============";boolean loop=true;double money=0;double balance=0;Date date=null;SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化信息do {System.out.println("===========零钱通菜单==========");System.out.println("\t\t\t1 零钱通明细");System.out.println("\t\t\t2 收益入账");System.out.println("\t\t\t3 消费");System.out.println("\t\t\t4 退 出");System.out.println("请选择(1-4)");key=scanner.next();switch (key) {case "1": System.out.println(detaile);break;case "2": System.out.println("收益为多少");money=scanner.nextDouble();//money的值范围应该校验balance+=money;//拼接收益入账信息date= new Date();//获取当前日期detaile+="\n收益入账\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);if (money<0) {System.out.println("\n输入的金额应该大于0\n");break;}break; case "3": System.out.println("3 消费金额");money=scanner.nextDouble();if (money<0||money>balance) {System.out.println( "\n你消费的金额应该在0--"+balance);break;}System.out.println("消费说明");String note=scanner.next();balance-=money;detaile+="\n"+note+"\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息break;case "4": String choice="";while(true) {System.out.println("你确定要退出吗 y/n");choice=scanner.next();if ("y".equals(choice)||"n".equals(choice)) {break;}}//判断是不是y或者n//在用户退出后判断y或Nif (choice.equals("y")) {loop=false;//退出}//看要不要退出System.out.println("4 退出");break;default:}}while (loop);System.out.println("========退出了零钱通项目=========");}}
知识
- 运用do while语句实现整个主体循环
- 运用switch语句来进行选择不同功能
- 运用if语句进行金钱的逻辑判断
面对过程
就是每一个类实现一个功能 代码
package new_pluse;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;public class oppo_change {Scanner scanner = new Scanner(System.in);String key="";String detaile=" ==============零钱通明细============";boolean loop=true;double money=0;double balance=0;Date date=null;SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化信息oppopublic void mainmenu() {do {System.out.println("===========零钱通菜单==========");System.out.println("\t\t\t1 零钱通明细");System.out.println("\t\t\t2 收益入账");System.out.println("\t\t\t3 消费");System.out.println("\t\t\t4 退 出");System.out.println("请选择(1-4)");key=scanner.next();switch (key) {case "1":this.detail();break;case "2":this.income();
// qpp.income(scanner, money, balance, detaile, sdf, date);break;case "3":this.consum();break;case "4":this.goout();break;}} while (loop);}public void detail() {System.out.println(detaile);}public void income() {System.out.println("收益为多少");money=scanner.nextDouble();if (money<0) {System.out.println("\n输入的金额应该大于0\n");return;}//money的值范围应该校验balance+=money;//拼接收益入账信息date= new Date();//获取当前日期detaile+="\n收益入账\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);}public void consum(){System.out.println("3 消费金额");money=scanner.nextDouble();if (money<0||money>balance) {System.out.println( "\n你消费的金额应该在0--"+balance);return;}System.out.println("消费说明");String note=scanner.next();balance-=money;detaile+="\n"+note+"\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);// break;}public void goout() {String choice="";while(true) {System.out.println("你确定要退出吗 y/n");choice=scanner.next();if ("y".equals(choice)||"n".equals(choice)) {break;}}//判断是不是y或者n//在用户退出后判断y或Nif (choice.equals("y")) {loop=false;//退出}//看要不要退出System.out.println("4 退出");return;}}
/*package new_pluse;import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;public class change_common{public static void main(String arg[]){//定义相关变量Scanner scanner = new Scanner(System.in);String key="";String detaile="1 ==============零钱通明细============";boolean loop=true;double money=0;double balance=0;Date date=null;SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//格式化信息do {System.out.println("===========零钱通菜单==========");System.out.println("\t\t\t1 零钱通明细");System.out.println("\t\t\t2 收益入账");System.out.println("\t\t\t3 消费");System.out.println("\t\t\t4 退 出");System.out.println("请选择(1-4)");key=scanner.next();switch (key) {case "1": System.out.println(detaile);break;case "2": System.out.println("收益为多少");money=scanner.nextDouble();//money的值范围应该校验balance+=money;//拼接收益入账信息date= new Date();//获取当前日期detaile+="\n收益入账\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);if (money<0) {System.out.println("\n输入的金额应该大于0\n");break;}break; case "3": System.out.println("3 消费金额");money=scanner.nextDouble();if (money<0||money>balance) {System.out.println( "\n你消费的金额应该在0--"+balance);break;}System.out.println("消费说明");String note=scanner.next();balance-=money;detaile+="\n"+note+"\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息break;case "4": String choice="";while(true) {System.out.println("你确定要退出吗 y/n");choice=scanner.next();if ("y".equals(choice)||"n".equals(choice)) {break;}}//判断是不是y或者n//在用户退出后判断y或Nif (choice.equals("y")) {loop=false;//退出}//看要不要退出System.out.println("4 退出");break;default:}}while (loop);System.out.println("========退出了零钱通项目=========");}}*/
使用一个菜单类menue
public void mainmenu() {do {System.out.println("===========零钱通菜单==========");System.out.println("\t\t\t1 零钱通明细");System.out.println("\t\t\t2 收益入账");System.out.println("\t\t\t3 消费");System.out.println("\t\t\t4 退 出");System.out.println("请选择(1-4)");key=scanner.next();switch (key) {case "1":this.detail();break;case "2":this.income();
// qpp.income(scanner, money, balance, detaile, sdf, date);break;case "3":this.consum();break;case "4":this.goout();break;}} while (loop);}
来模仿每个行为
然后分别用方法构建每个行为 如detail,consum,goout等
public void detail() {System.out.println(detaile);}public void income() {System.out.println("收益为多少");money=scanner.nextDouble();if (money<0) {System.out.println("\n输入的金额应该大于0\n");return;}//money的值范围应该校验balance+=money;//拼接收益入账信息date= new Date();//获取当前日期detaile+="\n收益入账\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);}public void consum(){System.out.println("3 消费金额");money=scanner.nextDouble();if (money<0||money>balance) {System.out.println( "\n你消费的金额应该在0--"+balance);return;}System.out.println("消费说明");String note=scanner.next();balance-=money;detaile+="\n"+note+"\t"+money+"\t"+sdf.format(date)+"\t"+balance;//拼接信息System.out.println(detaile);// break;}public void goout() {String choice="";while(true) {System.out.println("你确定要退出吗 y/n");choice=scanner.next();if ("y".equals(choice)||"n".equals(choice)) {break;}}//判断是不是y或者n//在用户退出后判断y或Nif (choice.equals("y")) {loop=false;//退出}//看要不要退出System.out.println("4 退出");return;}