1.算数运算符
1.1核心代码
System.out.println("请输入一个4位数:");
Scanner scanner1 = new Scanner(System.in);
int Input1 = scanner1.nextInt();
int singlenumber1 = Input1%10;
int tennumber1 = (Input1/10)%10;
int hundrednumber1 = (Input1/100)%10;
int kilobit1 = (Input1/1000)%10;
System.out.println("再输入一个4位数:");
Scanner scanner2 = new Scanner(System.in);
int Input2 = scanner2.nextInt();
int singlenumber2 = Input2%10;
int tennumber2 = (Input2/10)%10;
int hundrednumber2 = (Input2/100)%10;
int kilobit2 = (Input2/1000)%10;
System.out.println("结果是:" + (singlenumber1+singlenumber2+tennumber1+tennumber2+hundrednumber1+hundrednumber2+kilobit1+kilobit2));
1.2运行结果
2.拼接运算符
2.1核心代码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入名字:");
String name = scanner.next();
System.out.println("请输入年龄:");
String age = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入地址:");
String address = scanner.next();
System.out.println("请输入邮箱:");
String email = scanner.next();
System.out.print("我的名字是"+ name +",今年" + age + "岁,性别是" + gender + ",住在" + address + ",我的联系方式是:" + email);
2.2运行结果
3.自增自减过程分析
3.1代码分析
//定义a,b,c
int a,b,c;
//4赋值给a,5赋值给b,c赋值6;
a = 4;b = 5;c = 6;
//a++ 先运算后自增, (a++)==b 中a使用之前的值为4,b值为5,4不等于5,所以x为 false ;(这之后a=5,b=5,c=6)
boolean x = ((a++)==b);
//b++ 先运算后自增,而 --c 先自减后运算, (b++)==(--c) 中b使用之前的值为5,c使用自减后的值为5,5等于5,所以y为 true ;(这之后a=5,b=6,c=5)
boolean y = ((b++)==(--c));
//a=5,b=6,不相等,输出false
System.out.println(a==b);
//!x为 true ,y也为 true ,以 !x&y 为 true ,输出 true
System.out.println((!x&y));
4.switch实现用户登录
4.1核心代码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.next();
System.out.println("请输入密码");
int password = scanner.nextInt();
switch (username){
case "jack":
if (password == 123){
System.out.println("恭喜"+ username+"登录成功");
}else{
System.out.println("用户名正确,密码错误");
}break;
case "rose":
if (password == 456){
System.out.println("恭喜"+ username+"登录成功");
}else {
System.out.println("用户名正确,密码错误");
}break;
case "admin":
if (password == 789){
System.out.println("恭喜"+ username+"登录成功");
} else {
System.out.println("用户名正确,密码错误");
}break;
default:
System.out.println("用户输入错误");
}
4.2运行结果
4.2.1 用户1登录成功
4.2.2 用户2登录成功
4.2.3 用户3登录成功
4.2.4 用户名正确,密码错误
4.2.5 输入错误