1、算数运算符
==什么是运算符、什么是表达式==
运算符就是对常量或变量操作的符号;表达式的意思是用运算符把常量或变量连接起来符合java语法的式子
举例说明:
int a = 10; int b = 20; int c = a + b; 这里的 ‘=’ ‘+’ 都是运算符; a+b 是表达式
==特别注意单引号的+是算术运算符用于相加;而双引号的+是连接作用不是相加作用。如:==
int a = 10;
int b = 20;
int c = a+b;
System.out.println(c);// 输出结果是30
int a = 10;
int b = "20";
int c = a+b;
System.out.println(c);// 输出结果是1020
==案例:数值拆分==
需求:键盘录入一个三位数,将其拆分为个位、十位、百位后,打印在控制台
分析:
①:使用Scanner键盘录入一个三位数
②:个位的计算:数值 % 10
123 除以 10(商12,余数为3)
③:十位的计算:数值 / 10 % 10
123 除以 10 (商12,余数为3,整数相除只能得到整数)
12 除以 10 (商1,余数为2)
④:百位的计算:数值 / 100
123 除以 100 (商1,余数为23)
实现代码:
import java.util.*;
public class Day02_demo1{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int result = in.nextInt();
int ge = result%10;
int shi = result/10%10;
int bai = result/100%10;
System.out.println("您输入的数字的个位数是:"+ge);
System.out.println("您输入的数字的十位数是:"+shi);
System.out.println("您输入的数字的百位数是:"+bai);
}
}
int a = 1;
2、赋值运算符
3.关系运算符
4.赋值运算符
5、三元运算符
public class Day02_demo02 {
public static void main(String[] args) {
int height1 = 150;
int height2 = 210;
int height3 = 185;
int tempHeight = 0;
tempHeight = (height1>height2)?height1:height2;
System.out.println("最高的和尚是:"+((tempHeight>height3)?tempHeight:height3));
}
}
int c = 900;
System.out.println("最后的结果是:"+c);