JAVA语言基础案例总结

来源:互联网 发布:c语言异或条件 编辑:程序博客网 时间:2024/05/10 21:40
JAVA语言基础案例总结


1.类型提升问题
byte b1 = 3;
byte b2 = 4;
byte b3 = b1 + b2;//编译错误
byte b4 = 3 + 4;//编译正确
解释:(1)变量相加,首先首先进行类型提升,之后再进行计算,计算后将结果赋值;
 (2)常量相加,首先进行计算,之后判断是否在接受类型的范围,在则赋值。 


2.类型强制转换溢出结果计算
A:byte b = (byte)300;
 a: 300的二进制int类型表示
00000000 00000000 00000001 00101100
 b: 截断为byte类型(强转)
00101100
符号位 数值位
0 0101100
 c: 转为十进制数(正数原、反、补码相同)
符号位 数值位
补码 00101100
反码 00101100
原码 00101100
十进制数为: 44
 
B: byte b = (byte)130;
 a: 130的二进制int类型表示
00000000 00000000 00000000 10000010
 b: 截断为byte类型(强转)
10000010
符号位 数值位
1 0000010
 c: 转为十进制数
符号位 数值位
补码 10000010
反码 10000001
原码 11111110
十进制数为: -126
 
3.扩展赋值运算符隐含强制类型转换问题
判断下列代码是否有误,并指出错误
short s = 1;
s = s + 1; //错误,s在参加运算时会自动提示类型为int
// int类型值无法直接赋值于short类型

short z = 1;
z += 1; //正确,扩展赋值运算符包含强制类型转换
//等价于 z = (short)(z + 1);

4.位异或实现两变量交换值
int a = 10;
int b = 20;

a = a ^ b;
b = a ^ b; //b = a ^ b ^ b = a;
a = a ^ b; //a = a ^ b ^ a = b;

5.左移、右移、无符号右移的计算(针对二进制数的操作)
<< :左移,左边最高位丢弃,右边补齐0;
>> :右移,最高位是0,左边补齐0,最高位是1,左边补齐1;
>>> :无符号右移 无论最高位是0或1,左边补齐0.
a: -24 << 2
-24的二进制数为:10000000 00000000 00000000 00011000
反码: 11111111  11111111 11111111 11100111
补码:           11111111  11111111 11111111 11101000   
左移: (11)111111  11111111 11111111 1110100000
反码: 11111111 11111111 11111111 10011111
原码: 10000000 00000000 00000000 01100000
十进制为: -96
b: -24 >> 2
-24的二进制数为:10000000 00000000 00000000 00011000
反码: 11111111  11111111 11111111 11100111
补码:           11111111  11111111 11111111 11101000   
右移:     1111111111  11111111 11111111 111010(00)
反码: 11111111 11111111 11111111 11111001
原码: 10000000 00000000 00000000 00000110
十进制数为: -6
c: -24 >>> 2
-24的二进制数为:10000000 00000000 00000000 00011000
反码: 11111111 11111111 11111111 11100111
补码:           11111111  11111111 11111111 11101000   
无符右移:     0011111111  11111111 11111111 111010(00)
原码: 00111111  11111111 11111111 11111010
十进制数为: 1073741818

6.if语句和switch语句的应用场景
A: if
针对boolean类型的判断
针对一个范围的判断
针对几个常量的判断
B: switch
针对几个常量的判断

7.switch知识,看程序写结果(没有break,会发生case穿透现象)
A: int x = 2;
  int y = 3;
  switch(x){
default: //3.执行
y++; //4.y = 4
break; //5.停止
case 3: //1.不执行
y++;
case 4: //2.不执行
y++
  }
  System.out.println(y);//y = 4
B:  int a = 2;
int b = 3;
switch(a){
default: //3.执行
b++; //4.b = 4
case 3: //1.不执行    5.没有break,case穿透,继续执行
b++; //6.b = 5
case 4: //2.不执行    7.没有break,case穿透,继续执行
b++; //8.b = 6
}
System.out.println(b)//b = 6

8.switch语句的表达式可以是byte吗?可以是long吗?可以是String吗?
switch语句的表达式可以是:byte,short,int,char
JDK5之后可以是枚举
JDK7以后可以是String
答案:可以,不可以,JDK7以后可以

9.for循环打印阶乘问题
结果:  1!=1
1!+2!=3
1!+2!+3!=9
1!+2!+3!+4!=33
1!+2!+3!+4!+5!=153
代码:  int a = 1;
int b = 0;
String str = null;
for(int i=1;i<=5;i++){
a *= i;
b += a;
if(a==1){
str = a + "!";
}else{
str = str+"+"+i+"!";
}
System.out.println(str+"="+b);
}
10.while循环小芳存钱问题
问题:小芳的妈妈每天给她2.5元钱,她都会存起来,但是,每当这一天是存钱的第5天或者5的倍数的话,
 她都会花去6元钱,请问,经过多少天,小芳才可以存到100元钱。
代码:  double sum = 0;
int count = 1;
while (true){
sum += 2.5;
if(sum >= 100){
System.out.println("存100共用天数为:"+count);
break;
}
if(count%5 == 0){
sum -= 6;
}
count ++;
}

11.控制跳转语句问题
A: break:中断
用在循环和switch语句中,可跳出单层循环,配合标签语句可跳出多层循环
B: continue:继续
跳出单层循环的一次,循环可继续执行
C: return:返回
结束方法,遇到return,程序将不再继续往后执行
练习:补全代码
for(int i = 1;i <= 10;i++){
if(1%3 == 0){
//填写代码
}
System.out.println("hello");
}
如何在控制台输出2次:hello
break;
如何在控制台输出7次:hello
continue;
如何在控制台输出13次:hello
System.out.println("hello");


12.嵌套for循环问题
打印九九乘法表
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
int mul = y * x;
System.out.print(y + "*" + x + "=" + mul + "\t");
}
System.out.println();
}

13.数组的遍历
System.out.println("[");
for(int x = 0; x < arr.length; x++){
if(x == arr.length - 1){
System.out.println(arr[x] + "]");
}else{
System.out.println(arr[x] + ", ");
}
}
14.求数组中的最大值
int max = arr[0];
for(int x = 0; x < arr.length; x++){
if(arr[x] > max){
max = arr[x];
}
}
return max;


15.求数组中的最小值
int min = arr[0];
for(int x = 0; x < arr.length; x++){
if(arr[x] < min){
min = arr[x];
}
}
return min;

16.逆序
方式一:
for(int x = 0;x < arr.length/2; x++){
int temp = arr[x];
arr[x] = arr[arr.length - 1 -x];
arr[arr.length -1 -x] = temp;
}
方式二:
for(int start = 0,end = arr.length - 1;start <= end; start++,end--){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}


17.查找
方式一:
public static int getIndex(int[] arr,int value){
for(int x = 0; x < arr.length; x++){
if(arr[x] == value){
return x;
}
}
return -1;
}
方式二:
public static int getIndex(int[] arr,int value){
int index = -1;
for(int x = 0;x < arr.length; x++){
if(arr[x] == value){
index = x;
break;
}
}
return index;
}


18.二维数组遍历
public static void printArray(int[][] arr){
for(int x = 0; x < arr.length; x++){
for(int y = 0; y < arr[x].length; y++){
System.out.print(arr[x][y] + " ")
}
System.out.println();
}
}


19.二维数组求和
public static int sumArray(int[][] arr){
int sum = 0;
for(int x = 0; x < arr.length; x++){
for(int y = 0; y < arr[x].length; y++){
sum += arr[x][y];
}
}
return sum;
}
20.杨辉三角问题
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 
1 5 10 10 5 1
...
//创建二维数组
int[][] arr = new int[n][n];
//为第一列和第x行的第x列赋值为1
for(int x = 0; x < arr.length; x++){
arr[x][0] = 1;
arr[x][x] = 1;
}
//从第三行开始,为其他列赋值
for(int x = 2; x < arr.length; x++){
for(int y = 1; y < x-1; y++){
arr[x][y] = arr[x-1][y-1] + arr[x-1][y];
}
}
//遍历二维数组(注意:y的循环范围,对照乘法表)
for(int x = 0; x < arr.length; x++){
for(int y = 0; y < x; y++){
System.out.print(arr[x][y]+"\t");
}
System.out.println();
}
0 0
原创粉丝点击