作业1
求“斌”字的unicode码(至少使用两种方法)
public class What {
public static void main(String[] args) {
char a = '斌' ;
//使用自动类型转换
int res = a + 1 - 1 ;
System.out.println(res) ;
//使用强制数据类型转换
int res2 = (int) a ;
System.out.println(res2) ;
}
}
作业2
定义两个变量,交换这两个变量的值,不要少于三种方式
public class TestChange{
public static void main (String[] args) {
//交换两个数
int a=10,b=20;
System.out.println("交换前: a="+ a +"/ b="+b);
//第一种方法:临时变量法
/*
优点是运行时间快,缺点是多占据了一个空间
*/
//int temp =a;
//a=b;
//b=temp;
//第二种方法:数学法
/*
优点是不需要开辟新的空间
缺点是运行慢
时间复杂度和空间复杂度是成反比的
*/
a=a+b; //a=30
b=a-b; //b=10
a=a-b; //a=20
//第三种方法:异或法,后续学习
System.out.println("交换后:a="+ a +"/ b="+b);
}
}