例1:
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:
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);
}
}
例3:
public class Example3_3 {
public static void main(String args[]){
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语句");
}
}
例4:
enum Season{
春季,夏季,秋季,冬季
}
public class E {
public static void main(String args[]){
int x=96,y=1;
Season season=Season.春季;
String str=new String("java语言");
switch (x+y){
case 1:
System.out.println(x+y);
break;
case 'a':
System.out.println(x+y);
case 10:
System.out.println(x+y);
break;
default:System.out.println("没有般置的"+(x+y));
}
switch (season){
case 冬季:
System.out.println("隆冬腊月");
case 春季:
System.out.println("春天种下种子");
case 秋季:
System.out.println("秋天收获果实");
break;
case 夏季:
System.out.println("暑假真好");
}
switch (str){
case"Java语言":
System.out.println("学习Java语言");
case"C语言":
System.out.println("最好学习过C语言");
break;
}
}
}
例5:
public class Example3_5 {
public static void main(String args[]){
long sum=0,a=8,item=a,n=12,i=1;
for(i=1;i<=n;i++){
sum=sum+item;
item=item*10+a;
}
System.out.println(sum);
}
}
例6:
public class Example3_6 {
public static void main(String args[]){
double sum=0,item=1;
int i=1,n=20;
while (i<=n){
sum=sum+item;
i=i+1;
item=item*(1.0/i);
}
System.out.println("sum="+sum);
}
}
例7:
public class Example3_7 {
public static void main(String args[]){
int sum=0,i,j;
for(i=1;i<=10;i++){
if(i%2==0){
continue;
}
sum=sum+i;
}
System.out.println("sum="+sum);
for(j=2;j<=50;j++){
for(i=2;i<=j/2;i++){
if(j%i==0)
break;
}
if(i>j/2){
System.out.println(""+j+"是素数");
}
}
}
}
例8:
public class Example3_8 {
public static void main(String args[]){
int a[]={1,2,3,4};
char b[]={'a','b','c','d'};
for(int n=0;n<a.length;n++){
System.out.println(a[n]);
}
for(int n=0;n<b.length;n++){
System.out.println(b[n]);
}
for(int i:a){
System.out.println(i); }
for(char ch:b){
System.out.println(ch);
}
}
}
例9:
enum Color{
红,蓝,绿,黄,黑
}
public class Example3_9 {
public static void main(String args[]){
for(Color a:Color.values()){
for(Color b:Color.values()){
for(Color c:Color.values()){
if(a!=b&&a!=c&&b!=c){
System.out.print(a+","+b+","+c+"|");
}
}
}
}
}
}
例10:
enum Fruit{
苹果,梨,香蕉,西瓜,芒果
}
public class Example3_10 {
public static void main(String args[]){
double price=0;
boolean show=false;
for(Fruit fruit:Fruit.values()){
switch (fruit){
case 苹果:price=1.5;
show=true;
break;
case 芒果:price=6.8;
show=true;
break;
case 香蕉:price=2.8;
show=true;
break;
default:show=false;
}
if(show){
System.out.println(fruit+"500克的价格:"+price+"元");
}
}
}
}