第三天:程序逻辑-1(分支和循环)
- 程序的结构
- 流程图
- 分支结构
- 循环结构
- 多重循环结构
练习1:分段函数求值。
f(x)=⎧⎩⎨3x+5x−15x−3(x<−1)(−1≤x≤1)(x>1)
package com.lovoinfo;
import java.util.Scanner;
public class Fx {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("x = ");
double x = sc.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("f(x) = " + y);
sc.close();
}
}
练习2:个人所得税计算。在我国,个人所得税的起征点是3500元,计算公式是:
个人所得税 = (工资收入 - 五险一金 - 个税起征点) * 税率 - 速算扣除数
其中,税率和速算扣除数可以查下表得到:
级数 | 含税级距 | 税率 | 速算扣除数 |
---|---|---|---|
1 | 不超过1500元的 | 3 | 0 |
2 | 超过1500元至4500元的部分 | 10 | 105 |
3 | 超过4500元至9000元的部分 | 20 | 555 |
4 | 超过9000元至35000元的部分 | 25 | 1005 |
5 | 超过35000元至55000元的部分 | 30 | 2755 |
6 | 超过55000元至80000元的部分 | 35 | 5505 |
7 | 超过80000元的部分 | 45 | 13505 |
package com.lovoinfo;
import java.util.Scanner;
public class Tax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入工资: ¥");
double salary = sc.nextDouble();
double add = salary - salary * 0.08 - 3500;
double tax;
if(add <= 0) {
tax = 0;
}
else if(add <= 1500) {
tax = add * 0.03;
}
else if(add <= 4500) {
tax = add * 0.1 - 105;
}
else if(add <= 9000) {
tax = add * 0.2 - 555;
}
else if(add <= 35000) {
tax = add * 0.25 - 1005;
}
else if(add <= 55000) {
tax = add * 0.3 - 2755;
}
else if(add <= 80000) {
tax = add * 0.35 - 5505;
}
else {
tax = add * 0.45 - 13505;
}
System.out.printf("需要缴纳的个人所得税: ¥%.2f元\n", tax);
sc.close();
}
}
作业:输入一个百分制的成绩,将其转换成对应的等级,规则如下表所示:
成绩 | 等级 |
---|---|
90-100 | A |
80-89 | B |
60-79 | C |
<60 | D |
import java.util.Scanner;
public class OfAssignment {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入分数:");
double e = sc.nextDouble();
if (e>=0&&e < 60) {
System.out.println("D");
} else if (e > 60&&e < 80) {
System.out.println("C");
} else if (e >=80&&e < 90) {
System.out.println("B");
} else if (e >=90&&e <= 100) {
System.out.println("A");
}else if (e < 0||e > 100) {
System.out.println("你脑壳出问题了!百分制,懂不懂!");
}sc.close();
}
}
第二种做法:
import java.util.Scanner;
public class AProblem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("在这里输入:一个百分制数");
double fen = sc.nextDouble();
if (fen<0||fen>100) {System.out.println("尼玛这你都要输出来,不合理请重新输入:");
}else if (fen<60) {System.out.println("D");
}else if (fen<80) {System.out.println("C");
}else if (fen<90) {System.out.println("B");
}else if (fen<=100) {System.out.println("A");
}sc.close();
}
}//明显第二种方式简单多了,有时候解题的方式有多种,我们要选着简单的那一个!
练习4:输入成绩等级输出对应的评语,规则如下表所示:
成绩等级 | 评语 |
---|---|
A | 该生成绩优异,学习踏实认真 |
B | 该生积极上进,学习态度较好 |
C | 该生学习努力,成绩有待提高 |
D | 该生成绩稳定,动手能力很强 |
package com.lovoinfo;
import java.util.Scanner;
public class CommentSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入成绩等级: ");
char level = sc.next().charAt(0);
switch(level) {
case 'a':
case 'A':
System.out.println("该生成绩优异,学习踏实认真");
break;
case 'b':
case 'B':
System.out.println("该生积极上进,学习态度较好");
break;
case 'c':
case 'C':
System.out.println("该生学习努力,成绩有待提高");
break;
case 'd':
case 'D':
System.out.println("该生成绩稳定,动手能力很强");
break;
default:
System.out.println("瓜西西,输错了!");
}
sc.close();
}
}
练习5:将一颗色子掷60000次,统计每一面出现的次数。
package com.lovoinfo;
public class ThrowDie {
public static void main(String[] args) {
int f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0;
for(int i = 1; i <= 60000; i++) {
int face = (int)(Math.random() * 6 + 1);
switch(face) {
case 1: f1++; break;
case 2: f2++; break;
case 3: f3++; break;
case 4: f4++; break;
case 5: f5++; break;
case 6: f6++; break;
}
}
System.out.println("1点出现了" + f1 + "次");
System.out.println("2点出现了" + f2 + "次");
System.out.println("3点出现了" + f3 + "次");
System.out.println("4点出现了" + f4 + "次");
System.out.println("5点出现了" + f5 + "次");
System.out.println("6点出现了" + f6 + "次");
}
}