java基础5_运算符_语句_方法

认识了解运算符、逻辑运算、表达式、if else语句、for/while/do while等循环语句,递归运算













package java基础;
public class TestSwitch {
public static void main(String[] args) {
int i = 8;
switch(i) {//只能探测int类型值
case 8 :
case 3 :
case 2 :
System.out.println("C");
break;
case 9 :
System.out.println("D");
break;
default:
System.out.println("error");
}
}
}




package java基础;

public class Operator {
public static void main(String[] args) {
int i1 = 10,i2 = 20;
System.out.println(i1+i2++);//30 ++(--)在前时先运算再取值,在后时先取值在运算
System.out.println(i2++);//21
System.out.println(i2);//22
System.out.println(i1+(++i2));//33
System.out.println(i2);//23

boolean a,b,c;
a = true;
b = false;
System.out.println("a&b="+(a&b));//false 逻辑与
System.out.println("a|b="+(a|b));//true 逻辑或
System.out.println("a^b="+(a^b));//true 逻辑异或
System.out.println("!a="+(!a));//false 逻辑非
System.out.println("!b="+(!b));//true 逻辑非
System.out.println("a&&b="+(a&&b));//false 短路与
System.out.println("a||b="+(a||b));//true 短路或
int i=1,j=2;
boolean flag1 = (i>3)&&(i+j)>5;//计算i>3为false,&&整个结果为false,短路,后面的表达式不再计算
boolean flag2 = (i<2)||(i+j)>5;//计算i<2为true,||整个结果为true,短路,后面的表达式不再计算

int id = 800+90;//算术加法
String s = "hi"+"dog!";//连接运算符

int score = 80;
int x = -100;
System.out.println(score<60?"不及格":"及格");//及格
int flag = x>0?1:(x==1?0:-1);//有括号优先计算括号里的表达式
System.out.println(flag);//-1

if(x<0) {//如果x<0为true,则执行大括号里的语句,否则不执行
System.out.println(x);//执行后x=-100,
}
if(x>0) {
System.out.println(x);//x>0为false,该语句不执行
} else {
System.out.println(x);//执行后x=-100
}

long result = 0;
long f = 1;
for(int k=1;k<=10;k++) {
f = f*k;
result += f;
}
System.out.println("result="+result);//计算result=1!+2!+3!+……+10!

long r1 = 0;
for(int m=1;m<=99;m=m+2) {
r1 += m;
}
System.out.println(r1);//计算r1=1+3+5+……+99

int m = 3;
while(m>1) {//满足条件就循环执行,直到不满足条件
System.out.println(m);//打印3,换行打印2
m--;
}
do {//先执行,然后判断条件,条件满足则循环执行,不满足则停止执行
System.out.println("do m = "+m);//打印 do m = 1
m++;
} while (m<=1);


System.out.println("以下为break演示");
//以下执行结果为h=1,h=2,后w=1,h=1,h=2,后w=2,……h=1,h=2,后w=9
for(int w=1;w<10;w++) {//第一个for循环,break外面循环的外循环

//break所在的当前循环
for(int h=1;h<100;h++) {//第二个for循环,break所在的当前for循环
System.out.println("h="+h);//打印结果为h=1,h=2,当执行到if语句h==2时,break了,break所在的当前循环结束
if(h == 2) {
break;
}
}
//break所在的当前循环

System.out.println("w="+w);//break结束了当前循环,外层循环没有结束
}

System.out.println("以下为continue演示");
//以下执行结果为h=1,h=2,h=3,h=4,h=5后w=1,h=1,h=2,h=3,h=4,h=5后w=2,……h=1,h=2,h=3,h=4,h=5,后w=9
for(int w=1;w<10;w++) {//第一个for循环,continue外面循环的外循环

//continue所在的当前循环
for(int h=1;h<=5;h++) {//第二个for循环,continue所在的当前for循环
System.out.println("h="+h);//打印结果为h=1,h=2,当执行到if语句h==2时,continue,当前循环继续
if(h == 2) {
continue;
}
}
//continue所在的当前循环

System.out.println("w="+w);//continue表示当前循环继续
}
}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值