程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(3)

java运算符、判断、循环

发布于2020-11-19 20:36     阅读(1249)     评论(0)     点赞(14)     收藏(3)


运算符

算数运算符

+:加

-:减

*:乘

/:除

%:取余

++:自增

–:自减

int x = 10;
int y = 20;
int z = 4;
int c = 0;
int end = 0;
c = x+y;
System.out.println(c);
c = x-y;
System.out.println(c);
c = x*y;
System.out.println(c);
c = y/x;
System.out.println(c);
c = y%4;
System.out.println(c);
//20%4 = 0
//21%4 = 1

//先赋值,后自增 end = c ; c = c+1
end =c++;
//end = 0;c = 1
System.out.println(end);

//先自增,后赋值 c = c+1;end = c ;
//c=2;end = 2
end =++c;
System.out.println(end);

关系运算符

==:判断石佛相等
>=:大于等于
<=:小于等于
!=:不等于
>:大于
<:小于
boolean a = 10;
boolean b = 20;
System.out.println(a == b);//false
System.out.println(a <= b);//true
System.out.println(a != b);//false

逻辑运算符

&&:并且

a&&b : ture a、b都为ture 其他情况都为false

||:或者

a||b : false a、b都为false其他情况都为true

!:非

!a 如果a为true 则!a为false

boolean a = true;
boolean b = false;
System.out.println(a && b);//false
System.out.println(a || b);//true
System.out.println(!a);//false

运算符的优先级

//从大到小
( )
++;--
*;/;%
+;-

判断

if判断

if(boolean){
  //java语句
}else{
  //java语句2
}

//如果boolean为true 运行java语句,否则运行java语句2


if(boolean){
  //java语句
}else if(boolean2){
  //java语句2
}else{
  //java语句3
}
//如果boolean为true 运行java语句,否则判断如果boolean2为true 运行java2语句,否则运行java语句3

switch判断

int x = 3;

switch (x){
  case 1:
    System.out.println(x);break;
  case 2:
    System.out.println(x);break;
  case 3:
    System.out.println(x);break;
  case 4:
    System.out.println(x);break;
  case 5:
    System.out.println(x);break;
  default:
    System.out.println("x啥也不是");
}
//switch是在当有个int 可能为n个值;但又不知道是哪个时用
// break 结束,没有break时switch会忽略case,一直运行下去

if (x == 1){
  System.out.println(x);
}else if (x == 2){
  System.out.println(x);
}else if (x == 3){
  System.out.println(x);
}else if (x == 4){
  System.out.println(x);
}else if (x == 5){
  System.out.println(x);
}else{
  System.out.println("x啥也不是");
}
//该语句等同于上方switch语句

循环

for循环

for (int x = 0;x<10;x++){
  System.out.println(x);
}
//格式:for(int;boolean;算数运算){循环语句}

//第一次循环:x = 0;输出x = 0;x++;
//第二次循环:x = 1;输出x = 1;x++;
//...
//第十次循环:x = 9;输出x = 9;x++;
//第十一次循环:x = 10;结束。没得输出;

/*
总输出:
0
1
2
3
4
5
6
7
8
9
*/

while 循环

int x = 0;
while (x<10){
  System.out.println(x);
  x++;
}
//while循环可能循环0次
//第一次循环:x = 0;输出x = 0;x++;
//第二次循环:x = 1;输出x = 1;x++;
//...
//第十次循环:x = 9;输出x = 9;x++;
//第十一次循环:x = 10;结束。没得输出;

/*
总输出:
0
1
2
3
4
5
6
7
8
9
*/

do-while循环

int x = 0;
do {
System.out.println(x);
x++;
}while (x<10);

//先执行一次后再判断(至少执行一次)
//第一次循环:x = 0;输出x = 0;x++;
//第二次循环:x = 1;输出x = 1;x++;
//...
//第十次循环:x = 9;输出x = 9;x++;
//第十一次循环:x = 10;结束。没得输出;

/*
总输出:
0
1
2
3
4
5
6
7
8
9
*/

原文链接:https://blog.csdn.net/weixin_45824323/article/details/109791647



所属网站分类: 技术文章 > 博客

作者:长这么胖

链接:http://www.javaheidong.com/blog/article/1022/6550d242fc97d6bd47c3/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)