运算符
1.赋值运算符
x += 10;
System.out.println(x);
x *= y + 3;
System.out.println(x);
2.算数运算符
+-*/
3.关系运算符
< <= > >= == !=
4.逻辑运算符
& | !(逻辑取反)
boolean z = 5 > 3;
System.out.println(z);
System.out.println(!z);
System.out.println(z);
5.短路运算符
&& ||
5.其他运算符
- 三元条件运算符
a > b ? a : b
- 自加自减运算符(尽量少用 千万不要乱用)
int a = 5;
int b = a++; // ++把自身加1
- new / instanceof / . / [] / ()
() 其中一种可以用来强制转换
double x = 3.99999;
//强制类型转换
int a = (int) x;
System.out.println(a);
int b = 5;
double c = 10;
int d = (int) (b + c);
short e = 100;
//e = (short) (e + 1);
e += 1;
- 位运算
int a = 3;
System.out.println(a << 2); // 3 * 2^2
int b = 90;
System.out.println(b >> 2); // 90 / 2^2
int c = 3;
System.out.println(c << 33); // 33 % 32 余 1
//乘以31最快
int d = 3;
d = (d << 5) - d;
System.out.println(d);
debug
debug - 调试 / 捉虫,先在对应行进行断点操作,然后按F11进入debug进行调试
分支结构 if
分支结构(选择结构) - 可以让程序有多条执行路径
练习
- 练习1: 输入一个年份判断是不是闰年
Scanner input = new Scanner(System.in);
System.out.println("输入一个年份: ");
// 分支结构(选择结构) - 可以让程序有多条执行路径
if (input.hasNextInt()) {
int year = input.nextInt();
if (year > 0) {
if(year % 4 == 0 && year % 100 != 0 ||
year % 400 == 0) {
System.out.println(year + "年是闰年");
}
else {
System.out.println(year + "年不是闰年");
}
/* boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) ||
year % 400 == 0;
System.out.println(year + "年" + (isLeapYear ? "是" : "不是") + "闰年");*/
}
else {
System.out.println("年份必须是一个正整数!");
}
}
else {
System.out.println("输入错误");
}
input.close();
- 练习2: 输入三角形3条边,判断是否构成三角形,输出周长和面积
Scanner input = new Scanner(System.in);
System.out.println("请输入三角形的三条边的长度: ");
if (input.hasNextDouble()) {
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
boolean isValid = a > 0 && b > 0 && c > 0 && a + b > c && a + c > b && b + c > a;
// if (a + b > c && a + c > b && b + c > a)
if (isValid) {
double perimeter = a + b + c;
double half = perimeter / 2;
double area = Math.sqrt(half * (half - a) * (half - b) * (half -c));
System.out.printf("三角形的周长: %.2f\n", perimeter);
System.out.printf("三角形的面积: %.2f\n", area);
/*System.out.println("三角形的周长: " + a + b + c);
double s = (a + b + c) * 0.5;
System.out.println("三角形的面积: " + Math.sqrt(s * (s - a) * (s - b) * (s - c)));*/
} else {
System.out.println("这3条边不能构成三角形!");
}
} else {
System.out.println("输入错误!");
}
input.close();
- 练习3: 输入身高 (cm) 和体重 (kg) 判断身材是否正常。判断标准 身高
/*Scanner input = new Scanner(System.in);
System.out.println("请输入身高(m)和体重(kg): ");
if(input.hasNextDouble()){
double height = input.nextDouble();
double weight = input.nextDouble();
if(height > 0 && weight > 0){
double bmi = weight / (height * height);
if(bmi > 29.7){
System.out.println("肥胖");
}
else if (25.4 < bmi && bmi <29.7) {
System.out.println("超重");
}
else if (19.6 < bmi && bmi <25.4) {
System.out.println("正常");
}
else if (17.6 < bmi && bmi <19.6) {
System.out.println("偏瘦");
}
else {
System.out.println("消廋");
}
}
else {
System.out.println("身高和体重必须大于0");
}
}
else {
System.out.println("输入错误!");
}
input.close(); */
Scanner input = new Scanner(System.in);
System.out.print("请输入您的名字: ");
String name = input.nextLine();
System.out.print("请输入身高(cm): ");
int height = input.nextInt();
System.out.print("请输入体重(kg): ");
int weight = input.nextInt();
/* System.out.println("请输入性别(1.男,2.女): ");
int gender = input.nextInt();
int refValue = gender == 1 ? 110 : 120; */
System.out.print("请输入性别(\"男\"或者\"女\"): ");
String gender = input.next(); //若使用nextLine()会处理前面输入体重后的回车符,实际没有性别输入结束
int refValue = gender.equals("男") ? 110 : 120;
if (height - refValue >= weight) {
System.out.println(name + "是标准身材!");
}
else {
System.out.println(name + "是肥胖身材!");
}
input.close();
- 练习4: f(x) = { 3x+5 (x<-1) x-1 (-1<=x<=1) 5x-3 (x>1)
Scanner input =new Scanner(System.in);
System.out.print("x = ");
double x = input.nextDouble();
double y;
if (x < -1){
y = 3 * x + 5;
}
else if (x <= 1) {
y = x - 1;
}
else {
y = 5 * x - 3;
}
System.out.println("y = " + y);
input.close();
作业
- 作业1:输入年和月,输出这个月总共有几天
Scanner input = new Scanner(System.in);
System.out.print("请输入年份: ");
int year = input.nextInt();
if (year > 0) {
System.out.print("请输入第几月: ");
int month = input.nextInt();
if (1 <= month && month <= 12) {
boolean Thrity = month == 4 || month == 6 || month == 9 ||
month == 11;
if (Thrity) {
System.out.println(year + " 年 " + month + " 月 有30天");
} else if (month == 2) {
boolean refValue = year % 4 == 0 && year % 100 != 0 ||
year % 400 == 0;
if (refValue) {
System.out.println(year + " 年 " + month + "月 有29天");
} else {
System.out.println(year + " 年 " + month + " 月 有28天");
}
} else {
System.out.println(year + " 年 " + month + " 月 有31天");
}
} else {
System.out.println("月份输入范围必须在:1~12");
}
} else {
System.out.println("年份必须大于0");
}
input.close();
- 作业2:个人所得税计算。在我国,个人所得税的起征点是3500元。计算公式是:个人所得税 = (工资收入 - 五险一金 -个人起征点) * 税率 -速算扣除数
Scanner input = new Scanner(System.in);
System.out.print("请输入你的工资: ");
double wages = input.nextDouble();
System.out.println("请输入你需要缴纳的五险一金: ");
double pay = input.nextDouble();
double tax,range;
if(wages > 3500){
range = wages - 3500;
if(range < 1500){
tax = (wages - pay - 3500) * 0.03;
}
else if (range < 4500){
tax = (wages - pay - 3500) * 0.1 - 105;
}
else if (range < 9000) {
tax = (wages - pay - 3500) * 0.2 - 555;
}
else if (range < 35000) {
tax = (wages - pay - 3500) * 0.25 - 1005;
}
else if (range < 55000) {
tax = (wages - pay - 3500) * 0.3 - 2755;
}
else if (range <80000) {
tax = (wages - pay - 3500) * 0.35 - 5505;
}
else {
tax = (wages - pay - 3500) * 0.45 - 13505;
}
System.out.println("你的个人所得税是: " + tax + " 元");
}
else {
System.out.println("你不用缴纳个人所得税!");
}
input.close();
作业改正
- 作业1:
Scanner input = new Scanner(System.in);
System.out.print("请输入年和月: ");
int year = input.nextInt();
int month = input.nextInt();
if (year > 0 && month >= 1 && month <=12) {
int days;
if (month == 2) {
/* if (year % 4 ==0 && year % 100 !=0 || year % 400 == 0) {
days = 29;
}
else {
days = 28;
}*/
days = (year % 4 ==0 && year % 100 !=0 || year % 400 == 0) ? 29 : 28;
}
else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
}
else {
days = 31;
}
System.out.println(year + "年" + month + "月有" + days + "天");
}
else {
System.err.print("输入错误!!!"); // err 标准错误输出
}
input.close();
- 作业2:
Scanner input = new Scanner(System.in);
System.out.print("请输入月薪: ");
double salary = input.nextDouble();
System.out.print("请输入五险一金: ");
double insurance = input.nextDouble();
double diff = salary - insurance - 3500;
double rate, deduction; // 税率,
if (diff <= 0){
rate = 0;
deduction = 0;
}
else if (diff <= 1500) {
rate = 0.03;
deduction = 0;
}
else if (diff <= 4500) {
rate = 0.1;
deduction = 105;
}
else if (diff <= 9000) {
rate = 0.2;
deduction = 555;
}
else if (diff <= 35000) {
rate = 0.25;
deduction = 1005;
}
else if (diff <= 55000) {
rate = 0.3;
deduction = 2755;
}
else if (diff <= 80000) {
rate = 0.35;
deduction = 5505;
}
else {
rate = 0.45;
deduction = 13505;
}
double tax = diff * rate - deduction;
System.out.printf("本月应缴纳税款: ¥%.2f元\n", tax);
System.out.printf("本月实发工资: ¥%.2f元\n", salary - insurance -tax);
input.close();