练习:使用if分支结构,提示用户输入两个数,求出最大值:
import java.util.Scanner; public class IdentifyDemo09 { public static void main(String[] args) { //提示用户输入两个数据 Scanner sc = new Scanner(System.in); System.out.println("请输入第一个数:"); int num1 = sc.nextInt(); System.out.println("请输入第二个数:"); int num2 = sc.nextInt(); int max = num1; if(num1 <= num2){ max = num2; } System.out.println("最大值为:" + max); } }
练习:使用if -else 分支结构,提示用户输入一个整数,判断是负数,还是非负数
import java.util.Scanner; public class panduan { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); int number = sc.nextInt(); if(number >= 0){ System.out.println("你输入的是非负数"); }else{ System.out.println("你输入的是负数"); } } }
练习:
计算个人所得税:
根据用户输入的薪水计算个人所得税并打印出来,其中个税起征点为:5000元,具体规则如下:
个人所得税起征点每月5000元。个人所得税起征点为5000元/月或60000万元/年,工资范围以及税率:
import java.util.Scanner; public class Salary { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入您的薪水:"); int salary = sc.nextInt(); double salaryPrice = 0.0; if(salary <= 5000 && salary >0){ System.out.println("无需纳税"); }else if(salary <= 8000){ salaryPrice =(salary - 5000) * 0.03; }else if(salary <= 17000){ salaryPrice = (salary - 5000) * 0.10; }else if(salary <=30000){ salaryPrice = (salary - 5000) * 0.20; }else if(salary <= 40000){ salaryPrice = (salary - 5000) * 0.25; }else if(salary <= 60000){ salaryPrice = (salary - 5000) * 0.30; }else if(salary <=85000){ salaryPrice = (salary - 5000) * 0.35; }else{ salaryPrice = (salary - 5000) * 0.45; } System.out.println("最终你的个人所得税为:" + salaryPrice); } }
练习:提示用户输入月份,判断当月天数(不考虑闰年)
import java.util.Scanner; public class SwitchCaseDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入月份:"); int month = sc.nextInt(); switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("当月有31天"); break; case 4: case 6: case 9: case 11: System.out.println("当月有30天"); break; default: System.out.println("当月有28天"); } } }
练习:打印1-100之间的偶数
public class ForDemo01 { public static void main(String[] args) { for(int i = 2;i <= 100 ;i++,i++){ System.out.println(i + " "); } } }
练习:跑圈,跑完一圈提示用户是否还能跑,如果不能则结束
import java.util.Scanner; public class WhileDemo01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i =1; while(true){ System.out.println("你已经跑了第" + i + "圈,是否继续?"); String str = sc.next(); if("否".equals(str)){ System.out.println("不行了,跑不动了!"); break; }else if("是".equals(str)){ System.out.println("加油,加油,正在跑第" + i + "圈。"); i++; } } } }
练习:打印九九乘法表
public class JiuJiu { public static void main(String[] args) { for(int i = 1; i <= 9 ;i++ ){ for(int j = 1 ;j <= i ; j++){ System.out.print(j + "*" + i + "=" + i*j + " "); } System.out.println(); } } }