Java学习之旅(二)

Java学习之旅(二),第1张

Java学习之旅(二)

章目录

各种运算符及注意事项数据输入流程控制总结

一、各种运算符及注意事项

1.赋值运算符:

2.自增自减运算符:

注意:自增自减运算符参与 *** 作时,与变量的位置不同结果也将不同

3.关系运算符:

注意:*关系运算符的结果要不是ture要不是false

          *书写时 == 与 = 要区分开来

4.逻辑运算符:

 5.短路运算符:

注意:&&运算符左边false右边则短路不执行,||运算符如果左边ture右边则短路不执行

6.三元运算符:

案例--两只老虎 

public class OperatorTest01 {
            public static void main(String[] args){
                int weight1 = 180;
                int weight2 = 200;
                boolean n = weight1 == weight2 ? ture : false;
                    System.out.println("b:" +b);
            }
}

 

二、数据输入

1.导包:

 

2.创建对象:

          

 

3.接受数据:

 

 三、流程控制

顺序结构

       顺序结构是程序中最简单最基本的流程控制,没有特定的语法结构,按照代码先后顺序,依次执行。

分支结构

(1)if 语句

条件判断结构有:if 结构和 switch 结构。而 if 结构又可以分为 if 单分支结构、if-else 双分 支结构、if-else if-else 多分支结构。

if 结构 案例代码书写:

public class ifdemo {
    public static void main(String[] args) {
        int a =10 ; int b =20 ;
        if(a==b){
             System.out.println("a等于b");     
      }
        int c =10;
        if(a == c){
              System.out.println("a等于c")             
        }
         System.out.println("end");
    }
}

(2)switch 语句

switch 会根据表达式的值从相匹配的 case 标签处开始执行,一直执行到 break 处或者 
是 switch 的末尾。

switch 语句 案例代码:

public class switchdemo {
    public static void main(String[] args) {
        int month = (int)(Math.random()*12)+1;
        if(month==1||month==2||month==3||month==4
                ||month==5||month==6){
            System.out.println(month+"月,属于上半年");
        }else{
            System.out.println(month+"月,下半年");
        }
 
        switch (month){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                System.out.println(month+"月,属于上半年");
                break;
            default:
                System.out.println(month+"月,下半年");
                break;
        }
}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5717514.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存