Java基础知识04

运算符的使用以及底层补码的计算

1.int a = 6 --; 是否能输出a?

不能    6是常数,不能自减

2.分析以下代码的运行结果

System.out.println(5&9);
/*
* 5:	0000 0101 
* 9:	0000 1001 
*   	0000 0001		1
*/

System.out.println(5|9);
/*
* 5:	0000 0101 
* 9:	0000 1001 
*   	0000 1101		13
*/

System.out.println(5^9);
/*
* 5:	0000 0101 
* 9:	0000 1001 
*   	0000 1100		12
*/

System.out.println(~-5);
/*
* 5:	0000 0101 
* -5:	1000 0101
* 补码	1111 1011
* 反码	0000 0100	4		
*/

System.out.println(5<<2);
/*
* 5:	0000 0101 
* 		0001 0100	20
*/

System.out.println(-5<<2);
/*
* 5:	0000 0101 
* -5:	1000 0101
* 补码	1111 1011
* 		1110 1100
* 原码	1001 0100	-20	
*/

System.out.println(-5>>2);
/*
* 5:	0000 0000 0000 0000 0000 0000 0000 0101 
* -5:	1000 0000 0000 0000 0000 0000 0000 0101
* 补码	1111 1111 1111 1111 1111 1111 1111 1011
* 右移	1111 1111 1111 1111 1111 1111 1111 1110
* 原码	1000 0000 0000 0000 0000 0000 0000 0010		-2	
*/

System.out.println(-5>>>2);
/*
* 5:	0000 0000 0000 0000 0000 0000 0000 0101 
* -5:	1000 0000 0000 0000 0000 0000 0000 0101
* 补码	1111 1111 1111 1111 1111 1111 1111 1011
* 右移	0011 1111 1111 1111 1111 1111 1111 1110		1073741822	
*/

System.out.println(5>>2);
/*
* 5:	0000 0000 0000 0000 0000 0000 0000 0101 
* 右移: 0000 0000 0000 0000 0000 0000 0000 0001		1
*/

System.out.println(5>>34);
/*
* 5:	0000 0000 0000 0000 0000 0000 0000 0101 
* 右移: 0000 0000 0000 0000 0000 0000 0000 0001		1
*/

System.out.println(97=='a');	//true
System.out.println(5.0==5);		//true
System.out.println(4>5 ^ 'c'>'a');		//true
System.out.println((int)(char)(byte)-1);
/*
* -1:	1000 0000 0000 0000 0000 0000 0000 0001
* 补码: 	1111 1111 1111 1111 1111 1111 1111 1111
* byte(-1):1111 1111
* 原码:		1000 0001	-1
* char(-1):1111 1111 1111 1111		65535
* int(65535):	0000 0000 0000 0000 1111 1111 1111 1111		65535
**/

流程控制

1.分析以下几段代码的输出结果

int b = 5;
if(b > 4)
	System.out.println(b); 	//5
else
	b--;
 System.out.println(b);		//5
{
    int b = 5;
    if(b > 4)
        b--;
    	System.out.println("b大于4");		//编译报错,语法错误
    else {
        System.out.println("b不大于4"); 
    }
 }
int age = 45;
if(age > 20) {
	System.out.println("年轻人");		//年轻人
}else if(age>40){
  System.out.println("中年人");
}else if(age>60){
  System.out.println("老年人");
}
int a=80;
switch(a) {
  case 90:{
    System.out.println("优秀");
  }
  case 80:{
    System.out.println("一般");		//一般
  }
  case 60:{
    System.out.println("及格");		//及格
    break;
  }
  case 50:{ 
    System.out.println("基础弱");
  }
}
int count = 0;
while(count > 0);
{
  System.out.println(count);		//0
}
for (int i = 0; i < 5; i++) {
  System.out.print(i); 
	i*=0.1;
}
System.out.println("循环结束");

// 0 1 1 1 1 .....   (死循环)
for(int i=0;i<3;i++) {
    System.out.println(i);
    if(i==1) {
        continue;
    }
    System.out.println("continue后的输出语句");
}
for (int i = 0; i < 3; i++) {
    System.out.println(i);
    if (i == 1) {
        return;
    }
    System.out.println("return后的输出语句");
}

2.编写代码实现如下效果的九九乘法表

public class MultipleTable {
	public static void main(String[] args) {
		for(int i = 1; i <= 9; i ++) {
			for(int j = 1; j <= i; j ++) {
				System.out.print(j+"*"+i+"="+i*j+"\t");
			}
			System.out.println();
		}			
	}
}

3.判断101-200之间有多少个素数,并输出所有素数。(只能被1和它本身整除的自然数为素数)

public class IsSushu {
	public static void main(String[] args) {
		int count = 0;
		for(int i = 2; i <= 200; i ++) {
			int flag = 0;
			for(int j = 2; j <= Math.sqrt(i); j ++) {
				if(i%j != 0) {
					continue;
				}
				else {
					flag = 1;
					break;
				}
			}
			if(flag == 0) {
				System.out.print(i+"\t");
				if((++count)%10 == 0 ) {
					System.out.println();
				}
			}
		}
		System.out.println();
		System.out.println("在2到200中素数的个数是"+count+"个!");
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值