学习JAVA第五天

大一学生,自学java希望自己能坚持下去!

这是我做的第一个小应用!

这是一个收支记录小程序,没有运用数据库,只是用了一个变量记录数据,所以程序退出后,重新运行便会初始化。

    这是实现接口,以及对页面的设计,对框架的构建。

public class pm
{
    public static void main(String[] args)
    {
       int imonthy = 100;
       String all ="\t收支\t总金额\t收入\t说明\n";
      

while(true)
{
   
    System.out .println("----------收支记录软件----------");
    System.out .println("----------1:收支详情----------");
    System.out .println("----------2:收入详情----------");
    System.out .println("----------3:支出详情----------");
    System.out .println("----------4:退出软件----------\n");
    System.out.print("请输入(1-4):");

        char choose = Utility.readMenuSelection();

    if(choose=='1')
    {  
        System.out.println("--------收支详情--------");
       
        System.out.println(all);
       
    }else if(choose=='2')
    {
        System.out.println("--------收入--------");
        System.out.print("\t请输入金额:");
        int de =Utility.readNumber();
         imonthy = de+ imonthy;
        System.out.print("\t请输入来源:");
        String tell= Utility.readString();
        all += "\t收入"+'\t'+imonthy +'\t'+de +'\t'+tell+'\n';
        System.out.println(" ");
        System.out.println("\t存储完毕!\n");
        
    }else if(choose=='3')
    {
        System.out .println("--------支出--------");
        System.out.print("\t请输入金额:");
        int de1 =Utility.readNumber();//输入支出的金额
           
         if(de1>imonthy) // 支出金额不大于现有余额
         {
             System.out.println("余额不足");
             System.out.println("当前余额:"+imonthy);
            continue;
         }     
         imonthy =imonthy-de1;
        System.out.print("\t请输入用途:");
        String tell= Utility.readString();
        all += "\t支出"+'\t'+imonthy +'\t'+de1 +'\t'+tell+'\n';
        System.out.println(" ");
        System.out.println("\t取出完毕!\n");



    }else if(choose=='4')
    {
        System.out.print("----确认退出?(Y/N)");
        char exit =Utility.readConfirmSelection();
        if(exit == 'Y')
        {
            break;
        }
        
    }
    }
}
}
    

再随之建立一个工具类,对逻辑,细节的实现

import java.util.Scanner;
/**
Utility工具类:
将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
*/
public class Utility {
    private static Scanner scanner = new Scanner(System.in);
    /**
	用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
	*/
	public static char readMenuSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1);
            c = str.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4') {
                System.out.print("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }
	/**
	用于收入和支出金额的输入。该方法从键盘读取一个不超过4位长度的整数,并将其作为方法的返回值。
	*/
    public static int readNumber() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(4);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
	/**
	用于收入和支出说明的输入。该方法从键盘读取一个不超过8位长度的字符串,并将其作为方法的返回值。
	*/
    public static String readString() {
        String str = readKeyBoard(8);
        return str;
    }
	
	/**
	用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
	*/
    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误,请重新输入:");
            }
        }
        return c;
    }
	
	
    private static String readKeyBoard(int limit) {
        String line = "";

        while (scanner.hasNext()) {
            line = scanner.nextLine();
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }

        return line;
    }
}

只运用了基本的函数,循环。

运行结果

好啦,今天就到这里啦!

加油! 

 项目为课程作业!

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值