小结题1
java 中的八种基本型和其初始值
int short long byte 初始值 0
float double 初始值 0.0
char 初始 空
boolean 初始 false
Java各数据类型所占字节数(Java中汉字占几个字节)
类型 |
占用位数 |
取值范围 |
封装类型 |
boolean |
1位 |
|
Boolean |
char |
16位 |
2的16次方 |
Character |
byte |
8位 |
-128~+127 |
Byte |
short |
16位 |
-2的15次方~+2 的15次方 |
Short |
int |
32位 |
-2的31次方~+2 的31次方 |
Integer |
long |
64位 |
-2的63次方~+2 的63次方 |
Long |
float |
32位 |
|
Float |
double |
64位 |
|
Double
|
java 中封装类和基本数据类型之间的关系(char character)
封装类用来存储数据,增强功能,具体见http://blog.sina.com.cn/s/blog_4c848b5d01000bon.html
用递归计算s = 2! + 4! + 6! + 8! + 10!;
计算n! 的函数fun()
计算加法递归函数 fun2()
程序如下:
1 public class helloworld { 2 3 public static void main(String[] args) { 4 helloworld hd = new helloworld(); 5 6 7 System.out.println(hd.fun2(4)); 8 } 9 10 public int fun(int n){ 11 if(n <= 1){ 12 return 1; 13 } 14 15 return n * fun(n-1); 16 } 17 18 public int fun2(int n){ 19 if(n <= 2){ 20 return fun(2); 21 } 22 23 return fun(n) + fun(n-2); 24 } 25 26 }
编程题:
输入一个字符串,将其转化成十进制,再将这个数转化成二进制输出,出错是需要判断是数字太大还是非法字符
前几个问题都好解决 转成二进制可以用api中integer的toBinaryString() 实现,也可以用数字%2取余数得到到时最后那个就有点烦了
可以读取二进制数组长度,如果大于32则超出了最大值,(这个地方用正则表达式判断非法字符应该挺好的,但是不会。。。)
查了下调用Character的isDigit() ,isLetter();
1 public class Transfer { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 try{ 8 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 9 String str = br.readLine(); 10 while(null != str){ 11 12 char[] array = str.toCharArray(); 13 14 for(int i=0;i<array.length;i++){ 15 if(!Character.isDigit(array[i])){ 16 System.out.println("非法字符输入"); 17 return; 18 } 19 } 20 21 String bytes = Integer.toBinaryString(new Integer(str)); 22 byte[] string = bytes.getBytes(); 23 if(string.length>32){ 24 System.out.println("数字太大"); 25 return; 26 } 27 System.out.println(bytes); 28 str = br.readLine(); 29 } 30 }catch(Exception e){ 31 32 } 33 } 34 35 }
经验证可行,暂时没发现bug,如果有希望评论指正~~~
最后巩固下数据库查询的知识,多表查询什么的