一、选择结构
1、【switch】通过switch将成绩高于90分的,输出’优秀’;高于80分的输出’良好’;高于60分的输出’合格’,其他为不合格
public class Test01 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入学生成绩:");
int score = in.nextInt();
switch(score/10){
case 10:
case 9:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
case 6:
System.out.println("合格");
break;
default:
System.out.println("不合格");
}
}
}
2、【if-else-if-else】企业发放的奖金根据利润提成。
利润低于或等于10万元时,奖金可提10%;
利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
20万到40万之间时,高于20万元的部分,可提成5%;
40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,
高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,
从键盘输入当月利润,求应发放奖金总数?
public class Test1001 {
public static void main(String[] args) {
/**
* profit<=10 bonus 10%
* 10<profit<=20 <10 10% >10 7.5%
* 20<profit<=40 >20 5%
* 40<profit<=60 >40 3%
* 60<profit<=100 >60 1.5%
* profit>100 >100 1%
*/
Scanner in = new Scanner(System.in);
System.out.println("请输入当月利润");
// profit为利润,bonus为奖金
double profit = in.nextDouble();
System.out.println("当月利润为:"+profit);
double bonus = 0;
double bonus_1 = 10_0000 *0.1;
double bonus_2 = bonus_1 + 10_0000*0.075;
double bonus_4 = bonus_2 + 20_0000*0.05;
double bonus_6 = bonus_4 + 20_0000*0.03;
double bonus_10 = bonus_6 + 40_0000*0.015;
if (profit <= 10_0000) {
bonus = profit * 0.1;
}else if (profit <= 20_0000) {
bonus = bonus_1 + (profit-10_0000) * 0.075;
} else if (profit <= 40_0000) {
bonus = bonus_2 + (profit-20_0000) *0.05;
} else if (profit <= 60_0000) {
bonus = bonus_4 + (profit-40_0000)*0.03;
} else if (profit <= 100_0000) {
bonus = bonus_6 + (profit-60_0000)*0.015;
} else if (profit > 100_0000){
bonus = bonus_10 + (profit-100_0000)*0.01;
}
System.out.println("应发奖金为:"+bonus);
}
}
二、循环结构
1、【for】打印1~100之间所有奇数的和
public class Test02 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if(i%2!=0){
sum += i;
}
}
System.out.println("1~100之间所有的奇数和为"+sum);
}
}
2、【for】打印1~100之间所有是7的倍数的整数的个数及总和
public class Test03 {
public static void main(String[] args) {
int count = 0;
int sum =0;
for (int i = 1; i <=100; i++) {
if(i%7==0){
count++;
System.out.println(i);
sum += i;
}
}
System.out.println("1~100之间所有是7的倍数的整数的个数是"+count);
System.out.println("1~100之间所有是7的倍数的整数的和是"+sum);
}
}
3、【for】计算0-100之间的奇数和偶数和
public class ForDemo02 {
public static void main(String[] args) {
int oddSum = 0; //奇数和
int evenSum =0; //偶数和
for (int i = 0; i <=100; i++) {
if(i%2!=0){ //奇数
oddSum += i;
}else{
evenSum += i;
}
}
System.out.println("奇数和是"+oddSum);
System.out.println("偶数和是"+evenSum);
}
}
4、【for】用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%(5*3)==0){ //每行
System.out.println("\n");
}
}
}
}
5、【for】打印九九乘法表
public class ForDemo04 {
public static void main(String[] args) {
//打印九九乘法表
// 1、打印第一列
//2、把固定的1用一个循环包起来
//3、去掉重复项,i<=j
//4、调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
6、【while】计算1+2+…+100=?
public class WhileDemo03 {
public static void main(String[] args) {
//计算1+2+...+100=?
int i = 0;
int sum = 0;
while(i<=100){
sum += i;
i++;//注意i++不要丢
}
System.out.println(sum);
}
}
7、【while】输出1-100
public class WhileDemo01 {
public static void main(String[] args) {
//输出1-100
int i =0;
while(i<100){
i++;
System.out.println(i);
}
}
}