![在这里插入图片描述](https://img-blog.csdnimg.cn/20190516141222595.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3cyMDE5NDI0,size_16,color_FFFFFF,t_70
凡是可以使用三元运算符的都可以改写成if-else,反之不成立。如果两个都以使用,优先使用三元运算符
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190516131856134.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3cyMDE5NDI0,size_16,color_FFFFFF,t_70
!
练习:随意给出一个三位数的整数,打印显示它的个位数,十位数,百位数的值。
格式如下:
数字xxx的情况如下:
个位数:
十位数:
百位数:
例如:
数字153的情况如下:
个位数:3
十位数:5
百位数:1
*/
class AriExer{
public static void main(String[] args) {
int num = 187;
int bai = num / 100;
int shi = num % 100/10;//int shi = num / 10 %10;
int ge = num % 10;
System.out.println("百位为:" + bai);
System.out.println("十位为:" + shi);
System.out.println("个位为:" + ge);
}
}
3.##
/*
运算符之一:算术运算符
+ — + - * / % (前)++(后)++(前)–(后)-- +
*/
class AriTest{
public static void main(String[] args) {
//除号:/
int num1 = 12;
int num2 = 5;
int result1 = num1 / num2;
System.out.println(result1);//2
int result2 = num1 /num2 * num2;
System.out.println(result2);//10
double result3 = num1 / num2;
System.out.println(result3);//2.0
double result4 = num1 / num2 + 0.0;//2.0
double result5 = num1 /(num2 + 0.0);//2.4
double result6 = (double)num1 / num2;//2.4
double result7 = (double)(num1 / num2);//2.0
System.out.println(result5);
System.out.println(result6);
//%:取余运算
//结果的符号与被模数的符号相同
//开发中,经常使用%来判断能否被除尽的情况
int m1 = 12;
int n1 = 5;
System.out.println("m1 % n1 =" + m1 % n1);
int m2 = -12;
int n2 = 5;
System.out.println("m2 % n2 =" + m2 % n2);
int m3 = 12;
int n3 = -5;
System.out.println("m3 % n3 =" + m3 % n3);
int m4 = -12;
int n4 = -5;
System.out.println("m4 % n4 =" + m4 % n4);
//(前)++:先自增1,然后再运算
//(后)++:先运算,后自增1
int a1 = 10;
int b1 = ++a1;
System.out.println("a1=" + a1 + ",b1 = " + b1);
int a2 = 10;
int b2 = a2++;
System.out.println("a2 = " + a2 + ",b2 = " + b2);
int a3 = 10;
++a3;//a3++;
int b3 = a3;
//注意点:
short s1 = 10;
//s1 = s1 + 1;// 编译失败
//s1 = (short)(s1 + 1);//正确的
s1++;//自增1不会改变本身变量的数据类型
System.out.println(s1);
//问题:
byte bb1 =127;
bb1++;
System.out.println("bb1 ="+bb1);
//(前)--:先自减1,后运算
//(后)--:先运算,后自减1
int a4 = 10;
int b4 = --a4;
System.out.println("a4 = "+ a4 +",b4 = " + b4);
}
}