java从入门到弃坑第二天

    xiaoxiao2021-03-25  307

    1.java基础运算:对常量和变量进行操作的过程,把常量和变量用操作符连接起来称为表达式。 2.运算符:算术运算符:+,-,*,/,%,++,--。注:a++为先运算后自增,++a为先自增后运算。                                         exp:public static void main(String[] args) {                                                          int a1=3,a2=3;                                                          int b=a1++;                                                          int c=++a2;                                                          System.out.println(b);                                                          System.out.println(c);                                                }结果:b=3,c=4                  赋值运算符:=,+=。注:+=中隐含了强制类型转换,a+=20即为a=a+20,结果为a的数据类型。                                    关系运算符:==,>=,<=,>,<,!=。注:关系运算符返回结果为布尔类型。                  逻辑运算符:&,|,!, ^ 异或 ,&&,||。注:双与和双或有短路效果,即第一个判断可得出结果时,第二个表达式便不再进行判断。                  三目运算符:(比较表达式)?表达式1:表达式2. 比较表达式为true,返回表达式1,为false,返回表达式  2. 3.从键盘录入数据:A:导包:import java.util.Scanner;位置在class上面。                                 B:创建键盘录入对象:Scanner sc=new Scanner(System.in);                                 C:从键盘录入数据:int i=sc.nextInt(); 4.流程控制语句:A:顺序语句。                             B:选择结构:if(比较表达式){                                                          语句体};                                                     switch(表达式){                                                       case1:语句体1;                                                       break;                                                       ......                                                       default:语句体n+1;                                                       break;                                                       }注:语句体可为一条或多条语句,在遇到break时,才会退出switch,若语句体后没有switch,在执行该语句体后会执行下一条语句体直到遇到break,此种情况为case穿透。case后值为常量,不可重复 exp:                                           C:循环语句:for循环:for(初始化语句;判断条件语句;控制条件语句){                                                                    循环体语句;}                                                                                while循环:初始化语句;                                                                         while(判断条件语句){                                                                          循环体语句;                                                                          控制条件语句;}                                                      二者区别:控制条件语句中的变量,在for循环结束后便不能再被访问,而在while循环中即使循环结束,也可以在循环体外访问此变量。使用中通常for适合明确循环次数,while适合循环次数不明。 exp:                                                                         do...while语句:初始化语句;                                                                           do{                                                                                    循环体语句;                                                                                    控制条件语句;                                                                                 }while(判断条件语句)注:do...while循环至少会执行一次 在循环语句中,遇到break就中断整个循环,而遇到continue就退出本次循环。     强制类型转换:从小向大为默认转换,从大到小只能手动转换,而且有可能会引起精度损失。
    转载请注明原文地址: https://ju.6miu.com/read-105.html

    最新回复(0)