栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习笔记第七天:极其基础的家庭记账系统

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习笔记第七天:极其基础的家庭记账系统

今天知识点比较杂,总结了其中一小部分比较有用的,同时敲了一个记账系统来练练手。容错已经做好了。这个系统在我学到更多之后还会再完善的(比如日期的精准记录,记账明细罗列,记账备注...等)。

总结一下学到的知识点:

import java.util.*;
public class HelloWorld {
    public static void main(String[] a) {
        System.out.println("总结一些写小型项目遇到的小知识:");
        System.out.println("" +
        "1:int n = Integer.parseInt(str) : 将字符串转化为int型数据,double的为Double.parseDouble(),其他的类似n" +

        "2:String str = readKeyBoard(n) :从键盘中读取长度为n的字符串n" +

        "3:try catch finally:用于处理异常的语句,使用方法与switch case 类似n" +

        "4:NumberFormatException:将String 转换为数字类型,而该String格式不满足时的异常类型n" +

        "5:readKeyBoard(n).toUpperCase():读取长度为n的字符,并将字母转换为大写n" +

        "6:String.charAt(n),返回String类型字符串角标为n的字符的char型n" +

        "7:System.currentTimeMillis() 获取当前与1970年1月1日0时0分0秒的时间差,一般用于计算程序的运行时间" +

        "8:时停:try{ Tread.sleep(timeLength);} catch(Exception ignored){} ");
    }
}

main部分:

import java.util.*;

public class Main {
    private static final double[] incExp = new double[2];
    private static long days = 0;
    private static final Scanner sc = new Scanner(System.in);
    private static void nextLine(){ System.out.println();}

    public static void main(String[] args) {
        Appearance.sayHi();
        while(true){
            System.out.println("n记录收入支出请按 1;查看收入支出请按 2;退出记账系统请按 3:");
            System.out.print("请输入您的操作: ");
            String ins = sc.nextLine();
            nextLine();
            if(Objects.equals(ins, "1")){
                days += Inc_Exp_Statistic.exchange(incExp);
            }
            else if(Objects.equals(ins, "2")){
                Inc_Exp_Statistic.see(incExp, days);
            }
            else if(Objects.equals(ins, "3")){
                Appearance.sayGoodBye();
                return;
            }
            else{
                System.out.println("您输入的指令有误, 请重新操作!n");
            }
        }


    }
}

收支记录部分:

import java.util.Scanner;

public class Inc_Exp_Statistic {
    private static final Scanner sc = new Scanner(System.in);
    private static long getInt(){
        String str = sc.nextLine();
        long num = 0;
        try{
            num = Long.parseLong(str);
        }catch(NumberFormatException e){
            System.out.println("您输入的不是数字,请检查您的输入!");
            return -1;
        }
        return num;
    }
    private static double getDouble(){
        String str = sc.nextLine();
        double num = 0;
        try{
            num = Double.parseDouble(str);
        }catch(NumberFormatException e){
            System.out.println("您输入的不是数字,请检查您的输入!");
            return -1;
        }
        return num;
    }
    public static long exchange(double[] incExp){
        long days = 0;
        while (true) {
            System.out.print("请输入要统计的天数: ");
            long low = getInt();
            if(low == -1) continue;
            break;
        }
        while(true){
            System.out.print("请输入新增的收入: ");
            double low= getDouble();
            if(low == -1) continue;
            incExp[0] += low;
            break;
        }
        while(true){
            System.out.print("请输入新增的支出:");
            double low= getDouble();
            if(low == -1) continue;
            incExp[1] += low;
            break;
        }
        return days;
    }
    public static void see(double[] incExp, long days){
        System.out.println("统计天数:" + days + "n");
        System.out.printf("总入账:%.2fn",incExp[0]);
        System.out.printf("总支出:%.2fn",incExp[1]);
        double profit = incExp[0] - incExp[1];
        System.out.printf("净收入:%.2fn",profit);
        if(profit <= 0) {
            System.out.println("加油加油!从不向生活低头!n");
        }else{
            System.out.println("继续努力哦~n");
        }
    }
}

外观部分:

import java.util.concurrent.TimeUnit;

public class Appearance {
    private static void timePause(int duration){
        try{Thread.sleep(duration);}
        catch(Exception ignored){}
    }
    public static void sayHi(){
        System.out.println(
                "           T————————————家庭记账系统————————————Tn" +
                "           |                                  |n" +
                "           |  记录收入支出请按 1                 |n" +
                "           |                                  |n" +
                "           |  查看收入支出请按 2                 |n" +
                "           |                                  |n" +
                "           |  退出记账系统请按 3                 |n" +
                "           #__________________________________#");
    }
    public static void sayGoodBye() {
        System.out.println("感谢您的使用,再见~n");
        timePause(500);
        System.out.print(".");
        timePause(800);
        System.out.print(".");
        timePause(800);
        System.out.println(".");
        timePause(1200);
        System.out.println("真的再见了哦...");
        timePause(2000);
        System.out.println("n感谢您的使用,再见> <");
    }
}

输出展示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/936039.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号