[阶段一]Java基础语法-数据类型(2)

来源:互联网 发布:台州网络答题知识竞赛 编辑:程序博客网 时间:2024/04/30 19:37

课程内容

此次是在工作日的晚上上课,下班后就匆匆茫茫从软件园飞奔过来。看到学员早已就坐,心里甚是高兴,疲惫感瞬间烟消云散。仿佛看到当年初出茅庐的自己,那种求知若渴的眼神,恨不得快点把自己所学传授予他们。此次课程依旧着眼于数据类型,从浮点类型继续往下讲。

  1. 浮点类型 ——float 4字节 double 8字节
  2. 浮点数科学计数法——指数,尾数,基数,精度
  3. 浮点数运算——存在误差且比较慢
  4. 字符类型——char 2字节
  5. unicode字符编码—— 约8w+字符,是ASCII编码的超集
  6. 转义字符—— \n \t \ \’
  7. 布尔类型—— boolean (true or false)

课堂作业

编写程序,将浮点数转换成人民币读法,例如,将1006.33 转换成壹仟零陆元叁角叁分。(此次作业难度较高,涉及到之前所学到知识,以及后面将要学习的知识,希望通过这个作业做个桥梁)

package com.mashen;import java.util.Scanner;/* * 将浮点数值转换成汉字表示 * @author tony * @date 2016-5-22 * @version v1.0.0 */public class NumToStr {    //0~9汉字表示方式    private final String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};    //单位表示    private final String[] unitArr = {"仟", "", "十", "佰"};    private final String[] tag = {"元", "万", "亿"};    public static void main(String[] args) {        NumToStr ntr = new NumToStr();        System.out.println("请输入数字:");        Scanner scanner=new Scanner(System.in);        //将输入的值赋给num        double num=scanner.nextDouble();        //得到输出的结果,之后进行转换        String numStr=ntr.toHanStr(num);        System.out.println(numStr);    }    /*     * 转换成汉字字符串     * @param {double}输入的浮点数值     * @return {String}返回转换后的字符串     */    public String toHanStr(double num) {        String result = "";        //强制转换成long类型,得到整数部分        long zheng = (long)num;        //浮点数点去整数部分,得到的数再乘以100,得到的数再取整        long xiao = (long)(Math.round((num - zheng) * 100));        //需要考虑整数部分为0的情况,直接处理成角和分        if(0 == zheng) {            int tempJiao = (int)(xiao / 10);            int tempFen = (int)(xiao % 10);            if(tempJiao != 0) {                result += hanArr[tempJiao] + "角";            }            if(tempFen != 0) {                result += hanArr[tempFen] + "分";            }            return result;        }        String zhengStr = String.valueOf(zheng);        int len = zhengStr.length();        int tempLen = 0;        //重点是处理整数部分        for(int i = 0; i < len; i++) {            //在ASCII对照表中,字符和十进制数是相差48            int temp = zhengStr.charAt(i) - 48;            //当前字符处于哪个段            int part = (len - i - 1) / 4;             //当前字符处于该段的具体哪个位置            int location = (len - i - 1) % 4;             //不是该段的最后一个            if(location != 0) {                //并且当前字符不是0                if(temp != 0) {                    //需要添加单位                    result += hanArr[temp] + unitArr[(len - i) % 4];                     continue;                }                //当前字符为0                else {                     tempLen = result.length();                    //当前字符为0且为该段第一个                    if(3 == location && result.charAt(tempLen - 1) != '零') {                         result += "零"; continue;                    } else {                        tempLen = result.length();                        if(result.charAt(tempLen - 1) == '零') {                            continue;                        } else {                            result += "零"; continue;                        }                    }                }            }             //是该段最后一个            else {                 if(temp != 0) {                    result += hanArr[temp] + tag[part]; continue;                } else {                    tempLen = result.length();                    if(result.charAt(tempLen - 1) != '零') {                        result += tag[part]; continue;                    } else {                        if(result.charAt(tempLen - 2) == '亿' || result.charAt(tempLen - 2) == '万') {                            continue;                        } else {                            result = result.substring(0, tempLen - 1) + tag[part]; continue;                        }                    }                }            }        }        //处理小数部分,取整得到角,取余得到分        int jiao = (int)(xiao / 10);        int fen = (int)(xiao % 10);        if(jiao != 0) {            result += hanArr[jiao] + "角";        }        if(fen != 0) {            result += hanArr[fen] + "分";        }        return result;    }}

课件

03.Java基础语法-数据类型1.pptx

3 0
原创粉丝点击