一、判断各类型字符个数
描述
输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数
输入描述:
控制台随机输入一串字符串
输出描述:
输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母x数字x空格x其他x),预设代码中已给出输出
示例1
输入:
!@#¥% asdyuihj 345678
输出:
英文字母8数字6空格2其他5
题解:
import java.util.Scanner; public class Main { public static void main(String[] args) { int numbers = 0; int words = 0; int space = 0; int other = 0; Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); for(int i=str.length()-1; i>=0; i--){ char c = str.charAt(i); if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){ words++; }else if (c >= '0' && c <= '9'){ numbers++; }else if (c == ' '){ space++; }else{ other++; } } System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } }
二、编写个人所得税计算程序
个人所得税是国家对本国公民、居住在本国境内的个人的所得和境外个人来源于本国的所得征收的一种所得税。假设某地区的起征点为3500元(即月工资低于3500时不需要缴纳个人所得税),个人所得税的计算公式为:应纳税额=(工资薪金所得-扣除数)×适用税率-速算扣除数。其中,扣除数为3500元,适用税率以及速算扣除数如下表所示(注:此表并非当前国家个人所得税缴纳标准表,且为简化逻辑个人所得税的计算方式也进行了一定修改)
表-1 个人所得税缴纳标准
全月应纳税所得额 |
税率 |
速算扣除数(元) |
不超过1500元 |
3% |
0 |
超过1500元至4500元 |
10% |
105 |
超过4500元至9000元 |
20% |
555 |
超过9000元至35000元 |
25% |
1005 |
超过35000元至55000元 |
30% |
2755 |
超过55000元至80000元 |
35% |
5505 |
超过80000元 |
45% |
13505 |
上表中的全月应纳税所得额=工资薪金所得-扣除数。
现在请你新建三个employee对象小明,小军和小红,他们的月工资分别为2500,8000,100000。并将他们按照顺序存入集合中。遍历集合并计算他们应缴纳的个人所得税(个人所得税为double类型,保留一位小数)。
输入描述:
无
输出描述:
小明应该缴纳的个人所得税是:xxx
小军应该缴纳的个人所得税是:xxx
小红应该缴纳的个人所得税是:xxx
题解:
import java.util.*; public class Main { public static void main(String[] args) { List<Employee> employees = new ArrayList<>(); //write your code here...... Employee employee1 = new Employee("小明",2500); Employee employee2 = new Employee("小军",8000); Employee employee3 = new Employee("小红",100000); employees.add(employee1); employees.add(employee2); employees.add(employee3); for (int i = 0; i < employees.size(); i++) { double tax = 0.0; double taxIncome = employees.get(i).getSalary() - 3500; if (taxIncome <= 0) { tax = 0.0; } else if (taxIncome <= 1500) { tax = taxIncome * 0.03; } else if (taxIncome <= 4500) { tax = taxIncome * 0.10 - 105; } else if (taxIncome <= 9000) { tax = taxIncome * 0.20 - 555; } else if (taxIncome <= 35000) { tax = taxIncome * 0.25 - 1005; } else if (taxIncome <= 55000) { tax = taxIncome * 0.30 - 2755; } else if (taxIncome <= 80000) { tax = taxIncome * 0.35 - 5505; } else { tax = taxIncome * 0.45 - 13505; } System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:" + tax); } } } class Employee{ private String name; private int salary; public Employee(String name, int salary) { this.name = name; this.salary = salary; } public String getName() { return name; } public int getSalary() { return salary; } }
三、记录点赞用户
描述
为了实现社区点赞功能,要求设计一个点赞记录器,该工具包含如下两个方法:
1. like方法:该方法需要传入用户名作为参数,如果用户没点赞过,则记录本次点赞行为,若用户已经点赞过,则删除他的点赞行为。
2. getLikeUsers方法:该方法需要返回所有点赞用户的名字,不要求顺序。
(为保证答案正确,请使用HashSet完成本题)
输入描述:
用户名
输出描述:
所有点赞且未取消点赞的用户名,不要求顺序。(输出为Arrays.toString形式)
示例1
输入:
Tom Jim Lucy Lily Tom Lucy Tom
输出:
[Tom, Lily, Jim]
题解:
import java.util.*; public class Main { public static void main(String[] args) { LikeRecorder recorder = new LikeRecorderImpl(); Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String name = scanner.next(); recorder.like(name); } System.out.println(Arrays.toString(recorder.getLikeUsers())); } } /** * 点赞记录器 */ interface LikeRecorder { /** * 若用户没有点赞过,则记录此次点赞行为。 * 若用户曾经点赞过,则删除用户点赞记录。 * * @param username 用户名 */ void like(String username); /** * 返回所有点赞的用户名 * * @return 用户名数组 */ String[] getLikeUsers(); } class LikeRecorderImpl implements LikeRecorder { private HashSet<String> names = new HashSet(); @Override public void like(String name) { if (names.contains(name)) { names.remove(name); } else { names.add(name); } } @Override public String[] getLikeUsers() { return names.toArray(new String[0]); } // write your code here...... }
四、回文数判断
描述
请补全预设代码中判断回文数的方法,若输入的五位数字是回文是则返回true,否则返回false。回文数即个位与万位相同,十位与千位相同的数字。如:12321、89898
输入描述:
控制台输入的五位数
输出描述:
true/false
示例1
输入:
12321
输出:
true
示例2
输入:
18182
输出:
false
题解:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int number = console.nextInt(); //write your code here...... int arr[] = new int[5];// 创建一个大小为5的数组 int i = 0; do {// 逐次取位 arr[i] = number % 10; number /= 10; i++; } while (i < 5); if (arr[0] == arr[4] && arr[1] == arr[3]) { System.out.println("是回文数"); } else { System.out.println("不是回文数"); } } }
五、判断素数个数
描述
输入两个正整数,输出这两个正整数之间(左闭右闭,即判断包括这两个整数在内有多少素数)有多少个大于2的素数。如果start>end,则将start设为end,end设为start
输入描述:
两个正整数
输出描述:
start到end之间有count个大于2的素数
示例1
输入:
1 100
输出:
1到100之间有24个大于2的素数
示例2
输入:
100 1
输出:
1到100之间有24个大于2的素数
题解
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); method(start,end); } public static boolean isPeime(int num){ for(int i = 2; i * i <= num; i++){ //从2遍历到根号num if(num % i == 0) //一旦可以整除就不是素数 return false; } return true; } public static void method(int start,int end){ int count=0; if(start > end){ //如果start更大,则交换 int temp = start; start = end; end = temp; } for(int i = start; i <= end; i++){ //遍历start到end if(i <= 2) //不大于2的不要 continue; if(isPeime(i)) //判断这个数是否是素数 count++; //返回true,素数+1 } System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); //输出 } }
六、根据周长求面积
描述
已知:
1. 图形类Shape,该类中定义了图形的周长属性,以及返回周长的方法。
2. Area接口,该接口中定义了返回面积的方法getArea()。
要求:
1. 定义圆形类Circle,使其继承于Shape,并实现Area接口。
2. 定义方形类Square,使其继承于Shape,并实现Area接口。
注意:
圆周率要使用Math类中的常量。
输入描述:
周长
输出描述:
面积(计算时请使用Math类中的常量,面积为double类型,保留三位小数,四舍五入部分预设代码已经完成)
示例1
输入:
4
输出:
1.273 1.000
题解
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNextDouble()) { double s = scanner.nextDouble(); // Circle和Square是需要你定义的类 System.out.println(String.format("%.3f",new Circle(s).getArea())); System.out.println(String.format("%.3f", new Square(s).getArea())); } } } class Shape { private double s; // 周长 public Shape(double s) { this.s = s; } public double getS() { return s; } } interface Area { double getArea(); // 面积 } // 圆形 class Circle extends Shape implements Area { //write your code here...... public Circle(double s) { super(s); } @Override public double getArea() { return Math.PI * Math.pow((getS() / (2 * Math.PI)), 2); } } // 方形 class Square extends Shape implements Area { //write your code here...... public Square(double s) { super(s); } @Override public double getArea() { return Math.pow((getS() / 4), 2); } }