继续学习JavaSE的内容,更新的速度有一点慢了,之后会合理分配好时间,及时更新
1.续JavaSE02的内容,运算符
比较运算符-〉==,!=,<,>,<=,>=,instanceof:检查是否是类的对象
逻辑运算符->&(与),|(或),^(异或),!(非),&&(逻辑或),||(逻辑与)
2.各个比较运算符特点
&特点:
true & true =true;
true & false =false;
false & true =false;
false & false =false;
&运算规律:
&运算的两边只要有一个false,结果肯定为false
只有两边都为true,结果才为true
|特点:
true | true =true;
true | false =true;
false | true =true;
false | false =false;
|运算规律:
运算的两边只要有一个true,结果肯定为true
只有两边都为false,结果才为false
^特点:
true | true =false;
true | false =true;
false | true =true;
false | false =false;
^运算规律:
^符号两边相同,结果为false
两边的结果不同,结果为true
!特点:
!true=false
!false=true
!!ture=true
&&特点:
短路运算,&&左面一旦为假,右面就不执行了,&&左面为真,右面依然执行
&&与&的区别:&无论左边结果为啥,右边都参与运算
||特点:
短路运算,||左面一旦为真,右面就不执行了,||左面为假,右面依然执行
&&与&的区别:&无论左边结果为啥,右边都参与运算
3.位运算符及其计算
位运算符-><<(左移),>>(右移),>>>(无符号右移),&,|,^,~(反码)
位运算是二进制运算
<<(左移):
规律:左移几位就是该数乘以2的几次方
<<:可以完成2的次幂运算
>>(右移):
6>>1:6向右移1位(最高位是啥就用什么补位),110->11=3
6>>2:6向右移2位(最高位是啥就用什么补位),110->1=1
规律:
6/2^1=3,6/2^2=6/4=1
>>:右移几位就是除以2的几次幂
>>>(无符号右移):
>>>:数据进行右移时,高位出现空位,无论高位是什么,空位都用0补
4.三元运算符
(条件表达式)?表达式1:表达式2
条件为true,运算结果为表达式1
条件为false,运算结果为表达式2
5.程序流程控制
顺序结构
判断结构
选择结构
循环结构
if和switch的应用
if:
1.对具体的值进行判断
2.对区间判断
3.对运算结果是boolean类型的表达式进行判断
switch:
1.对具体的值进行判断
2.值的个数通常是固定的
对于固定的只判断,建议使用switch语句,switch将答案加载进内存
6.什么时候使用循环结构?
某些代码执行多次,使用循环结构
一个条件进行一次判断,使用if
一个条件进行多次判断,使用while
循环通常情况下,需要定义条件,需要控制次数
7.比较运算符案例代码
class OperateDemo3{
public static void main(String[] args){
//比较运算符,运算结果必须是true或者false
System.out.println(3>2); //结果为true
System.out.println(3<2); //false
System.out.println(3==2); //false
System.out.println(3!=2); //true
//逻辑运算符用于连接 boolean类型的表达式
int x=3;
System.out.println(x>2&x<5);
System.out.println(x<2 | x>5);
}
}
8.逻辑运算符案例代码
class OperateDemo4{
public static void main(String[] args){
System.out.println(6&3); //110&011->010=2
System.out.println(6|3); //110|011->111=7
//一个数异或同一个数两次,结果还是这个数,6^3^3=6
System.out.println(6^3); //110^011->101=5,101^011->110
System.out.println(6|3); //110|011->111=7
System.out.println(~6); //异或:取反在加一,结果为-7
System.out.println(3<<2);//3往左移两位 11->1100=12(低位补两个0)
//3*2^2=12
System.out.println(3<<3);//3往左移三位 11->11000=24(补三个0)
//3*2^3=24
}
}
9.三元运算符案例代码
class OperateDemo5{
public static void main(String[] args){
int x=3,y;
y=(x>1)?100:200;
System.out.println(“y=”+y);
//获取两个整数中较大的数
int a=3,b=-2;
int max=a>b?a:b;
System.out.println(“max=”+max);
//获取三个整数中较大的数
int o=2,p=3,q=1;
int temp=o>p?o:p;
int max2=temp>q?temp:q;
System.out.println(“max2=”+max2);
}
}
10.操作符相关案例代码
class OperateTest{
public static void main(String[] args){
//最有效的算出2乘以8的结果
System.out.println(2<<3); //2*2^3
//对两个整数变量的值进行互换
int a=3,b=5;
System.out.println(“a=”+a+”and”+”b=”+b);
//方法一 阅读性强
/* int c;
c=a;
a=b;
b=c; */
//方法二
//不推荐使用这种方式,如果两个整数值过大,会超出int范围,会强制转换,数据发生变化
/*
a=a+b; //a=8
b=a-b; //b=8-5=3
a=a-b; //8-3=5 */
//方法三 面试时
a=a^b; //a=3^5
b=a^b; //b=3^5^5=3
a=a^b; //a=3^5^3=5
System.out.println(“a=”+a+”and”+”b=”+b);
}
}
11.if语句案例
class IfDemo{
public static void main(String[] args){
/* if语句的第一种格式
if(条件表达式){
执行语句;
}*/
/*
int x=3;
if(x>1){
System.out.println(“yes”);
}
System.out.println(“over”);
*/
int x=1;
if(x>1){
if(x<2){
System.out.println(“yes”);
System.out.println(“over”);
}
}
System.out.println(“no”);
}
}
class IfDemo2{
public static void main(String[] args){
/* if语句的第二种格式
if(条件表达式){
执行语句;
}else
{
执行语句;
}
*/
int x=1;
if(x>1){
System.out.println(“yes”);
}else{
System.out.println(“over”);
}
int a=3,b;
if(a>1)
b=100;
else
b=200;
//b=a>1?100:200 三元运算符就是if else语句简写格式
System.out.println(“b=”+b);
}
}
class IfDemo3{
public static void main(String[] args){
/* if语句的第二种格式
if(条件表达式){
执行语句;
}else if(条件表达式)
{
执行语句;
}else{
执行语句;
}
*/
int x=3;
if(x>1){
System.out.println(“a”);
}else if(x>2){
System.out.println(“b”);
}else if(x>3){
System.out.println(“c”);
}
else{
System.out.println(“d”);
}
int y=3;
if(y>1)
System.out.println(“a”);
if(y>2)
System.out.println(“b”);
if(y>3)
System.out.println(“c”);
else
System.out.println(“d”);
//嵌套if
if(x==1){
if(y==1){
System.out.println(“d”);
} else{
System.out.println(“c”);
}
}
else{
if(y==1){
System.out.println(“d”);
} else{
System.out.println(“c”);
}
}
if(false); //结束了
//int m=89;
{//局部代码块
int m=89; //作用域,当前有效,用完释放内存空间(局部变量的生命周期)
System.out.println(“hello world”+m);
}
System.out.println(“over”+m);
}
}
12.if语句具体案例代码
class IfTest{
public static void main(String[] args){
//根据用户输入的数据,判断该数据对应的星期
int week=8;
if(week==1){
System.out.println(week+”是星期一”);
}
else if(week==2){
System.out.println(week+”是星期二”);
}
else if(week==3){
System.out.println(week+”是星期三”);
}
else if(week==4){
System.out.println(week+”是星期四”);
}
else if(week==5){
System.out.println(week+”是星期五”);
}
else if(week==6){
System.out.println(week+”是星期六”);
}
else if(week==7){
System.out.println(week+”是星期一”);
}
else{
System.out.println(“没有对用的星期”);
}
if(week==1){
System.out.println(week+”是星期日”);
}
}
}
class IfTest2{
public static void main(String[] args){
//根据输入的月份,给出对应得季节
/*
int month=2;
if(month==3||month==4||month==5){
System.out.println(month+”月是春季”);
}else if(month==6||month==7||month==8){
System.out.println(month+”月是夏季”);
}else if(month==9||month==10||month==11){
System.out.println(month+”月是秋季”);
}else if(month==12||month==1||month==2){
System.out.println(month+”月是冬季”);
}
*/
int month=8;
if(month>1||month<12)
if(month>=3&&month<=5){
System.out.println(month+”月是春季”);
}else if(month>=6&&month<=8){
System.out.println(month+”月是夏季”);
}else if(month>=9&&month<=11){
System.out.println(month+”月是秋季”);
}else{
System.out.println(month+”月是冬季”);
}
}
}
13.switch语句代码
class SwitchDemo{
public static void main(String[] args){
/*
switch(表达式){
case 取值1:
执行语句;
break;
case 取值2:
执行语句;
break;
…
default:
执行语句;
break;
}*/
/* int x=3;
switch(x){
case 1:
System.out.println(“a”);
break;
case 2:
System.out.println(“b”);
break;
case 3:
System.out.println(“c”);
break;
default:
System.out.println(“d”);
break;
}
*/
int x=5;
switch(x){
default:
System.out.println(“d”);
// break;
case 1:
System.out.println(“a”);
//break;
case 2:
System.out.println(“b”);
// break;
case 3:
System.out.println(“c”);
break;
case 4:
System.out.println(“e”);
break;
}
/*
int a=4,b=2;
char opr=’*’;
switch(opr)
{
case ‘+’:
System.out.println(a+b);
break;
case ‘-‘:
System.out.println(a-b);
break;
case ‘*’:
System.out.println(a*b);
break;
case ‘/’:
System.out.println(a/b);
break;
default:
System.out.println(“无法计算”);
break;
}
*/
}
}
14.switch具体案例代码
class SwitchTest{
public static void main(String[] args){
//用户输入的数据对应出星期
/*
int week=2;
switch(week){
case 1:
System.out.println(“星期一”);
break;
case 2:
System.out.println(“星期二”);
break;
case 3:
System.out.println(“星期三”);
break;
case 4:
System.out.println(“星期四”);
break;
case 5:
System.out.println(“星期五”);
break;
case 6:
System.out.println(“星期六”);
break;
case 7:
System.out.println(“星期日”);
break;
case default:
System.out.println(“没有对应星期”);
break;
}
*/
int month=8;
switch(month){
case 3:
case 4:
case 5:
System.out.println(“春季”);
break;
case 6:
case 7:
case 8:
System.out.println(“夏季”);
break;
case 9:
case 10:
case 11:
System.out.println(“秋季”);
break;
case 12:
case 1:
case 2:
System.out.println(“冬季”);
break;
default:
System.out.println(“没有对应季节”);
break;
}
}
}
15.while语句案例代码
class WhileDemo{
public static void main(String[] args){
/*
while(条件表达式){
执行语句;
}*/
//死循环(cmd中:ctrl+c结束)
int x=1;
while(x<3){
System.out.println(“x=”+x);
x++;
}
}
}
class WhileTest{
public static void main(String[] args){
//获取1到10个数字的和
int i=1;
int sum=0;
while(i<=10){
sum=sum+i;
i++;
}
System.out.println(“sum=”+sum);
}
}
class WhileTest2{
public static void main(String[] args){
//1到100之间6的倍数出现的次数
int i=1;
int count=0;
while(i<=100){
if(i%6==0)
count++;
i++;
}
System.out.println(“count=”+count);
}
}
16.do while案例代码
class DoWhileDemo{
public static void main(String[] args){
/*
do{
执行语句;
}while(条件表达式)
*/
//do while:无论条件是否满足,语句至少执行一次
int x=1;
do{
System.out.println(“x=”+x);
x++;
} while(x<1);
int y=1;
while(y<1){
System.out.println(“y=”+y);
y++;
}
}
}
17.for语句案例代码
class ForDemo{
public static void main(String[] args){
/*
for(初始化表达式;循环条件表达式;循环后的操作表达式){
执行语句;(循环体)
})
*/
/*
for(int i=0;i<3;i++){
System.out.println(“i=”+i);
}*/
//正常不会这样写,为了理解也可以这样写
int i=1;
for(System.out.println(“a”);i<3;System.out.println(“c”)){
System.out.println(“d”);
i++;
}
// 也有这样的 for(int i=0,j=0;i<3;i++,j–)
}
}
class ForTest{
public static void main(String[] args){
int sum=0;
for(int i=0;i<=10;i++){
sum=sum+i;
}
System.out.println(“sum=”+sum);
/*
for和while的特点
1.for和while可以互换
2.格式不同
*/
//打印十个数字
int x=1;
while(x<5){
System.out.println(“x=”+x);
x++;
}
System.out.println(“x====”+x);
for(int i=0;i<5;i++){
System.out.println(“i=”+i);
}
// System.out.println(“i====”+i);
//无限循环最简单形式
//while(true){}
//for(;;){}
}
}
所有代码经过在下亲测,均可运行成功,请自行运行!查看结果,更有利于理解!欢迎给出意见或建议!!!
Latest posts by zchao (see all)
- Auraでアクションボタン作成して画面のチェックボックス項目一括処理 - 2021年4月12日
- デフォルト項目値を含むレコード作成実例説明(defaultFieldValues) - 2021年1月9日
- Salesforce のノーコード・ローコード開発 - 2020年12月31日
转载请注明:zchao博客之家 » Java入门学习–JavaSE03