java 基础复习 --小知识点

Java复习小知识点

2014611日 星期三

1、赋值运算

Byte  s=4

// s=s+4; 编译失败

原因:它进行两步操作,先运算后赋值,s会被提升为int类型,运算的结果还是int类型无法赋值给short类型

 

S+=4; //编译成功

原因:它进行一次赋值操作,+=在给s赋值自动完成了强转操作。

 

 

2、异或^ :  两边相同为false,不同为true

3&&&的比较:

&:无论左边是true还是false,右边都运算

&&:左边为false时,右边不运算。

 

 单或和双或的比较:

: 两边都参与运算

|| :当左边为true是,右边不运算

 

4、位运算





小练习:

1、以最有效的方式算出2*8等于几?2<<3;

2、对两个整数变量的值进行互换(不要求第三方变量)

Int  m= 8 , n=3;

1)使用第三方变量(常用)

int  temp;

temp=n;

n=m;

m=temp;

 

(2) 不使用第三方变量
n=n+m; //如何nm的值非常大容易超int类型的范围

m=n-m;

n=n-m;

(3) 使用异或运算

n=n^m;

m=n^m; //(n^m)^m

n=n^m; //n^(n^m)

System.out.println(“m”+m, “n”+n);

 

 

 

6、switch分支选择特点:

*只接受byteshortintchar型变量

*casedefault的位置可以随意调,只有在case中没有确定的值时才执行default结果。

*结束执行:遇到break,或执行到语句末尾。

*如果匹配的case或者default没有对应的break,则继续向下执行直到遇到break或者执行到语句末尾。

例:输出月份

int x=4;

switch(x){

case 3;

case 4;

case 5;

System.out.println(x+"春季");

break

case 6;

case 7;

case 8;

System.out.println(x+"夏季");

break

case 8;

case 9;

case 10;

System.out.println(x+"秋季");

break

case 11;

case 12;

case 1;

System.out.println(x+"冬季");

break

default 

System.out.println("输入错误!");

}

 

一般情况下,如果是以上四种类型的话就选择switch,比if要高效。当结果为Boolean或者区间的时候则用if较为方便

 

七、whilefor的小区别:

1、变量有自己的作用域,对于for来讲,如果用于控制循环的增量定义在for语句中,那么该变量旨在for语句中执行完毕,该变量在内存中被释放。

2、whilefor可以互换,如果需要定义循环增量则for循环更合适些。

总结:什么时候使用循环结构?
当要对某些语句执行很多次时就使用循环结构。

 

3、for循环中的表达式是不唯一的,而且初始值可以是一般表达式比如:

  int x=1;
		for(System.out.println("a");x<3;System.out.println("c"),x++){
			System.out.println("d");
			//x++;
  		}//adcdc

4、最简单的无限循环形式是?

for(;;){}

while(true){}

练习:1)打印1~10的和

package tan;
public class testforloop {
	public static void main(String[] args) {
	int sum=0;
	for(int i=1;i<11;i++){	
		sum=sum+i;		
	}
	System.out.println("sum:"+sum);
	}	
}

这就是累加思想:通过变量来记录循环操作后的结果。

  2)打印1~100之间7的倍数的个数

package tan;
public class testforloop {
	public static void main(String[] args) {
	/**打印1~100之间7的倍数,并统计个数
	 * 思路:
	 * 1、对1~100进行循环遍历
	 * 2、在遍历中,定义条件,只对7的倍数进行操作	
	 * 3、7的倍数变化是不确定的,因此只要符合条件,要用一个变量来记录这个变化的次数
	 *   该变量随着7的倍数的出现而自增。
	 */		
  int x=0;
  for(int i=1;i<=100;i++){
	  if(i%7==0){
		  System.out.println("i="+i);
		  x++;
	  }
  }
  System.out.println("共有7的倍数"+x+"个");
  }	
}

这就是计数器思想:通过变量来记录数据的状态变化。

八、for循环语句嵌套类型
   打印   *****

          ****

  ***

          **

          *

//打印三角形,可以定义变量x,也可以不用
	for(int i=0;i<5;i++){
		for(int j=i;j<5;j++){
			System.out.print("*");
			
		}
		System.out.println();	
	}		

打印

*

**

***

****

*****

for(int i=0;i<5;i++){
		for(int j=0;j<=i;j++){
			System.out.print("*");
			
		}
		System.out.println();	
	}		

打印

1

12

123

1234

12345

  for(int i=1;i<=5;i++){
		for(int j=1;j<=i;j++){
			System.out.print(j);
			
		}
		System.out.println();	}		

打印九九乘法表

1x1=1

1x2=2 2x2=4

1x3=3 2x3=6 3x3=9

1x4=4 2x4=8 3x4=12 4x4=16

1x5=5 2x5=10 3x5=15 4x5=20 5x5=25

1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36

1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49

1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64

1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

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

打印出如下图形:

    * 

   * * 

  * * * 

 * * * * 

* * * * * 

程序示例:

package tan;
public class testforloop {
	public static void main(String[] args) {
	for(int i=0;i<5;i++){
		//打印空格
		for(int j=i+1;j<5;j++){
			System.out.print(" ");
		}
		//打印*
		for(int z=0;z<=i;z++){
			System.out.print("* ");
		}
		System.out.println();
	}						
  }	
}

breakcontinue的区别比较:

package tan;
public class testforloop {
	public static void main(String[] args) {
	//break  w为标号可随意设置
	w:for(int i=0;i<3;i++){
		for(int j=0;j<4;j++){
			System.out.println("i="+i);
			break w;
		}
	}
				
//	continue只能作用域循环结构,结束本次循环继续下一次循环。
	for(int i=1;i<10;i++){
		if(i%2==0){
			continue;
		}
		System.out.println(i);//输出10以内的奇数
	}		
  }	
}

//注意break和continue单独存在时,下面可以有任何语句,但是下面的语句都执行不到

九、函数

1)返回真假值函数

package tan;
public class testforloop {
	public static void main(String[] args) {	
	System.out.println(compare(5,5));
  }	
public static boolean compare(int a,int b){
	return a==b;//代码优化最简单的方式

	/*if(a==b)
		return true;
	else false;*/
	
	//return (a==b)?true:false;

}
}

2)定义一个功能用于打印矩形

package tan;
public class testforloop {
	public static void main(String[] args) {
	System.out.println(compare(6,3));
	drawDr();
	draw(6,6);
	drawDr();
	draw(8,9);
	drawDr();
  }	
	
public static void draw(int row,int col){
	for(int i=0;i<row;i++){
		for(int j=0;j<col;j++){
			System.out.print("*");
		}
		System.out.println();
	}
}

public static boolean compare(int a,int b){
	return a==b;//代码优化最简单的方式
}

public static void drawDr(){
	System.out.println("---------------------------");
}

}

3)定义一个功能用于打印九九乘法表

package tan;
public class testforloop {
	public static void main(String[] args) {
		print9x9();
  }	
	
	public static void print9x9(){
		for(int i=1;i<10;i++){
			for(int j=1;j<=i;j++){
			System.out.print(j+"x"+i+"="+(i*j)+"\t");
				
			}
			System.out.println();	
		}		
	}
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值