运算符、常用类、流程控制
程序员文章站
2022-05-02 08:19:04
...
目录
一、运算符
1.算术运算符
加、减、乘、除、取模
public class Test01_operator {
public static void main(String[] args) {
int a = 12;//正号可以省略
int b = 1+3;
String str = "abc";
System.out.println(str + 5 + a + b);//有字符串的时候+表示拼接
int c = 10/3;
System.out.println(c);//此时运行结果是3,进行取整操作
int d = 10%3;
System.out.println(d);//正常运算的取余
}
}
自增、自减
public class Test02_operator {
public static void main(String[] args) {
int a = 10;
a++;//a+1
System.out.println(a);//11
int b = 20;
int c = b++;//先赋值,然后自身再增加;int c =b; b = b+1;
System.out.println("b="+b+";c="+c);//21;20
int d = 20;
int e = ++d;//先加1,在赋值: d = d+1; int e = d;
System.out.println("d="+d+";e="+e);//21;21
}
}
2.赋值运算符
public class Test03_operator {
public static void main(String[] args) {
int a = 10;
a += 1;//a = a + 1;
System.out.println(a);
}
}
3.比较运算符
public class Test04_operator {
public static void main(String[]args) {
System.out.println(1==1);//true
System.out.println(1<2);//true
System.out.println(3>4);//false
System.out.println(3<=4);//true
System.out.println(3>=4);//false
System.out.println(3!=4);//true}
}
4.逻辑运算符
public class Test05_operator {
public static void main(String[] args) {
boolean a = (1>2)&(3>2);
System.out.println(a);//false
boolean a1 = (1>2)|(4>3);
System.out.println(a1);//true
boolean a2 = (1>2)^(3>2);//不同为true,相同为false
System.out.println(a2);
boolean a3 = !true;
System.out.println(a3);
boolean a4 = (1>2)&&(3>2);//dead code死代码
System.out.println(a4);
boolean a5 = (2>1)||(3>4);//dead code死代码
System.out.println(a5);
}
}
5.三目运算符
三目运算符格式:
数据类型 变量名=布尔类型表达式?结果1:结果2
public class Test06_operator {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = a>b?a:b;//先判断a是否大于b,如果成立那么结果就是a,反之结果就是b
System.out.println(c);
}
}
二、常用类
1.键盘录入Scanner
1.导包 import java.util.Scanner;
位置:在package下面,并且在class的上边
2.创建对象/工具
Scanner sc = new Scanner(System.in);
3.获取数据
int num = sc.nextInt()
public class Test07_Scanner {
public static void main(String[] args) {
System.out.println("请输入你的姓名");
Scanner sc = new Scanner(System.in);
String name = sc.next();//一行
System.out.println("你的姓名是:"+name);
}
}
2.随机数Random
public class Test08_Random {
public static void main(String[] args) {
Random ra = new Random();
int a = ra.nextInt();//随机数
int b = ra.nextInt(100);//随机生成0~100的数
System.out.println(a);
System.out.println(b);
}
}
三、流程控制
1.判断语句
1)判断语句_if
if(关系表达式){
语句体;
}
public class Test09_if {
public static void main(String[] args) {
int a = 10;
if(a>9){
System.out.println("到底谁厉害");
}
}
}
2)判断语句_if…else
if(关系表达式) {
语句体1;
} else {
语句体2;
}
public class Test10_if_else {
public static void main(String[] args) {
int score = 80;
if(score>=60){
System.out.println("万事大吉");
}else
System.out.println("不及格,你可要注意了");
}
}
3)判断语句_if…elseif…else
if(判断条件1) {
执行语句1;
} else if(判断条件2) {
执行语句2;
}
…
} else if(判断条件n) {
执行语句n;
} else {
执行语句n+1;
}
public class Test11_if_elseif {
public static void main(String[] args) {
int score = 80;
if(score>90){
System.out.println("优秀");
}else if(score>=80&&score<90){
System.out.println("中等");
}else if(score<80&&score>=70){
System.out.println("良");
}else if(score<70&&score>=60){
System.out.println("及格");
}else{
System.out.println("上点心了");
}
}
}
2.选择语句–switch
switch(表达式) {
case常量值1:
语句体1;
break;
case常量值2:
语句体2;
break;
…
default:
语句体n+1;
break;
}
public class Test12_switch {
public static void main(String[] args) {
int level = 3;//1 黑铁 2黄金 3 珀金 4 钻石 5王者
switch(level) {
case 1:
System.out.println("黑铁");
break;
case 2:
System.out.println("黄金");
break;
case 3:
System.out.println("珀金");
break;
case 4:
System.out.println("钻石");
break;
case 5:
System.out.println("王者");
break;
default ://默认的情况,就相当于上面的都没有匹配项
System.out.println("手残党");
}
}
}