第三天
java2 实用教程第五版 耿祥义 张跃平编著
第三章代码
代码1:课本P36
public class Example3_1 {
public static void main(String[] args) {
char a1='十',a2='点',a3='进',a4='攻';
char secret = 'A';
a1 = (char) (a1^secret);
a2 = (char) (a2^secret);
a3 = (char) (a3^secret);
a4 = (char) (a4^secret);
System.out.println("密文:"+a1+a2+a3+a4);
a1 = (char) (a1^secret);
a2 = (char) (a2^secret);
a3 = (char) (a3^secret);
a4 = (char) (a4^secret);
System.out.println("原文:"+a1+a2+a3+a4);
}
}
运行结果:
/*****************************
密文:匀烸辚敺
原文:十点进攻
*****************************/
代码2:课本P39
public class Example3_2 {
public static void main(String[] args) {
int a=9,b=5,c=7,t=0;
if(b<a) {
t=a;
a=b;
b=t;
}
if(c<a) {
t=a;
a=c;
c=t;
}
if(c<b) {
t=b;
b=c;
c=t;
}
System.out.println("a="+a+",b="+b+",c="+c);
}
}
运行结果:
/*****************************
a=5,b=7,c=9
*****************************/
代码3:课本P40
public class Example3_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int math=65,English=85;
if(math>60) {
System.out.println("数学及格了!");
}
else {
System.out.println("数学不及格!");
}
if(English>90) {
System.out.println("英语是优!");
}
else {
System.out.println("英语不是优!");
}
System.out.println("我在学习if-else语句!");
}
}
运行结果:
/***************************
数学及格了!
英语不是优!
我在学习if-else语句!
****************************/
代码4:课本P42
在这里插入代码片
import java.util.Scanner;
public class Example3_4 {
public static void main(String args[]) {
int number=0;
System.out.println("输入正整数(按回车确定)");
Scanner reader = new Scanner(System.in);
number = reader.nextInt();
switch(number) {
case 11:
System.out.println("您未中奖!");
break;
case 111:
System.out.println("中奖数字"+number+",恭喜您,"+"您中