Android day3:Java浮点类型、转义字符、布尔类型

来源:互联网 发布:ic卡扇区数据分析转换 编辑:程序博客网 时间:2024/05/16 18:15

因为想要把作业一起发布,这一次的博客写的比较晚,原本打算昨晚就发的,可是发现作业还有一些问题,调了很久才完成。先回顾一下第三次课的内容吧!

1、浮点类型

(1)float:占4个字节,即32位,其中1位为符号位,8位为指数位,23位为尾数(精度)。虽然float类型表示范围要比int类型要广,可是精度却要比int弱,因为int类型1位为符号位,31位精度。(浮点类型字面量为double类型,声明float需要在字面量后加f,如:float num = 2.345f。

(2)double(默认):占8个字节,即64位,其中1位为符号位,11位为指数位,52位为尾数(精度)。虽然double类型表示范围要比long类型要广,可是精度却要比long弱,因为int类型1位为符号位,63位精度。

(3)char字符类型:字面量由英文字母/字符/汉字组成,并用单引号包括。

(4)Unicode编码:java底层使用Unicode来表示字符,2个字节(16位)表示一个字符,Unicode编码可以说是ASCII(范围0-127,8位)的超集,英文部分与它兼容,66表示字符A.......,97表示字符a.......。

(5)转义字符:常用(“\n"换行,”\t“制表位字符,” \" “双引号,” \' “单引号)(规律:\+相应符号)

(6)布尔类型:true/false(boolean isPass = true)


这一次的作业比较有难度,需要思考很久,因为比较迟才开始做,所以有些想法都未能实现,吸取教训,以后无论什么都尽快完成。

作业内容:编写程序,将浮点数转换成人民币读法,例如,将1006.33 转换成壹仟零陆元叁角叁分。

作业:

package control;import java.util.Scanner;public class HomeWork4 {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);System.out.println("请输入一个金额:");double yuan = input.nextDouble();int quzheng = (int)yuan;if(yuan-quzheng > 0){ yuan = yuan*100;}long zhengshu = (long)yuan;long yu = zhengshu%10;long shang = zhengshu/10;//获取输入的数的整数部分的位数int wei = 1;long weiShang = shang;if(weiShang >= 10){wei++;while(weiShang >= 10){wei++;long next = weiShang;weiShang = next/10;}}else{wei++;}if(zhengshu<10){wei=1;}//System.out.println(wei);//将输入的数存进数组long[] save = new long[wei];if(shang < 10){save[0] = yu;if(zhengshu>10)save[1] = shang;}for(int i = 0; shang >= 10; i++){save[i]=yu;long next = shang;shang = next/10;yu = next%10;if(shang <= 10){save[i+1] = yu;save[i+2] = shang;}}//尝试输出顺序输出//for(int i = save.length-1; i>=0; i--){//System.out.print(save[i]);//}System.out.println("读法为:");String[] dangWei = {"分","角","","拾","佰","仟","","拾","佰","仟","","十","佰","仟"};  //12for(int i = wei-1,a=1; i>=0; i--,a++){String n = String.valueOf(save[i]);/*int转String1》String.valueOf(i)2》 Integer.toString(i)3》 i+""*/if(n.equals("0") && i>=1 && ((save[i-1]!=0 && save[i+1]==0)||(save[i-1]!=0 && save[i+1]!=0)) ){String ling ="null";if(i!=0)ling = "零";if(yuan-quzheng==0){if(wei>=6){if(save[4]==0 && save[3]!=0 && save[5]!=0 && a+4==wei){ling="";}if(wei>=10){if(save[8]==0 && save[7]!=0 && save[9]!=0 && a+8==wei){ling="";}}}}if(yuan-quzheng>=0){if(wei>=8){if(save[6]==0 && save[5]!=0 && save[7]!=0 && a+6==wei){ling="";}if(wei>=12){if(save[10]==0 && save[9]!=0 && save[11]!=0 && a+10==wei){ling="";}}}}System.out.print(ling);}if(yuan-quzheng == 0){switch (n) {//case "0":System.out.print("零");//break;case "1":System.out.print("壹");System.out.print(dangWei[i+2]);break;case "2":System.out.print("贰");System.out.print(dangWei[i+2]);break;case "3":System.out.print("叁");System.out.print(dangWei[i+2]);break;case "4":System.out.print("肆");System.out.print(dangWei[i+2]);break;case "5":System.out.print("伍");System.out.print(dangWei[i+2]);break;case "6":System.out.print("陆");System.out.print(dangWei[i+2]);break;case "7":System.out.print("柒");System.out.print(dangWei[i+2]);break;case "8":System.out.print("捌");System.out.print(dangWei[i+2]);break;case "9":System.out.print("玖");System.out.print(dangWei[i+2]);break;}if(save.length>=1){if(i==0){System.out.print("元");}if(save.length>=5){if(i==4 ){System.out.print("萬");}if(save.length>=9){if(i==8){System.out.print("亿");}}}}if(yuan-quzheng==0){if(wei>=6){if(save[4]==0 && save[3]!=0 && save[5]!=0 && a+4==wei){System.out.print("零");}if(wei>=10){if(save[8]==0 && save[7]!=0 && save[9]!=0 && a+8==wei){System.out.print("零");}}}}if(yuan-quzheng>=0){if(wei>=8){if(save[6]==0 && save[5]!=0 && save[7]!=0 && a+6==wei){System.out.print("零");}if(wei>=12){if(save[10]==0 && save[9]!=0 && save[11]!=0 && a+10==wei){System.out.print("零");}}}}}if(yuan-quzheng != 0){switch (n) {//case "0":System.out.print("零");//break;case "1":System.out.print("壹");System.out.print(dangWei[i]);break;case "2":System.out.print("贰");System.out.print(dangWei[i]);break;case "3":System.out.print("叁");System.out.print(dangWei[i]);break;case "4":System.out.print("肆");System.out.print(dangWei[i]);break;case "5":System.out.print("伍");System.out.print(dangWei[i]);break;case "6":System.out.print("陆");System.out.print(dangWei[i]);break;case "7":System.out.print("柒");System.out.print(dangWei[i]);break;case "8":System.out.print("捌");System.out.print(dangWei[i]);break;case "9":System.out.print("玖");System.out.print(dangWei[i]);break;}if(save.length>=1+2){if(i==0+2){System.out.print("元");}if(save.length>=5+2){if(i==4+2){System.out.print("萬");}if(save.length>=9+2){if(i==8+2){System.out.print("亿");}}}}}}}}

0 0
原创粉丝点击