JAVA45 判断各类型字符个数
描述
输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数
输入描述:
控制台随机输入一串字符串。(代表数字的ASCII码值为48-57)
输出描述:
输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母x数字x空格x其他x),预设代码中已给出输出。
示例:
输入:!@#¥% asdyuihj 345678 输出:英文字母8数字6空格2其他5
分析:
1.熟知字符的ASCII码。
代码:
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();
//write your code here......
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)>=65 && str.charAt(i)<=90 || str.charAt(i)>=97 && str.charAt(i)<=122){
words++;
}else if (str.charAt(i)>=48 && str.charAt(i)<=57){
numbers++;
}else if(str.charAt(i)==32){
space++;
}else {
other++;
}
}
System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
}
}
大佬代码:
直接使用字符来判断。
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);
}
}
使用函数。
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(Character.isLetter(c)){
words++;
}else if (Character.isDigit(c)){
numbers++;
}else if (Character.isWhitespace(c)){
space++;
}else{
other++;
}
}
System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
}
}
使用正则表达式。
//总字符个数
int totalLen = str.length();
//英文字母个数
words = totalLen - str.replaceAll("[a-zA-Z]", "").length();
//数字个数
numbers = totalLen - str.replaceAll("[0-9]", "").length();
//空格个数
space = totalLen - str.replaceAll("[\\s]", "").length();
//其他字符的个数
other = totalLen - words - numbers - space;
JAVA46 编写个人所得税计算程序
描述
个人所得税是国家对本国公民、居住在本国境内的个人的所得和境外个人来源于本国的所得征收的一种所得税。假设某地区的起征点为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
分析:
1.编写三个类的对象。
2.ArrayListd添加元素。
3.计算个人所得税,再输出。
代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
//write your code here......
//编写三个Employee对象
Employee employee1=new Employee("小明",2500);
Employee employee2=new Employee("小军",8000);
Employee employee3=new Employee("小红",100000);
employees.add(employee1);
employees.add(employee2);
employees.add(employee3);
double tax=0;
for (int i = 0; i < employees.size(); i++) {
if(employees.get(i).getSalary()-3500<=1500){
tax=(employees.get(i).getSalary()-3500)*0.02-0;
if(tax<0){
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:0.0");
}else {
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}
}else if(employees.get(i).getSalary()-3500<=4500){
tax=(employees.get(i).getSalary()-3500)*0.1-105;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}else if(employees.get(i).getSalary()-3500<=9000){
tax=(employees.get(i).getSalary()-3500)*0.2-555;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}else if(employees.get(i).getSalary()-3500<=35000){
tax=(employees.get(i).getSalary()-3500)*0.25-1005;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}else if(employees.get(i).getSalary()-3500<=55000){
tax=(employees.get(i).getSalary()-3500)*0.3-2755;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}else if(employees.get(i).getSalary()-3500<=80000){
tax=(employees.get(i).getSalary()-3500)*0.35-5505;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}else {
tax=(employees.get(i).getSalary()-3500)*0.45-13505;
System.out.println(employees.get(i).getName()+"应该缴纳的个人所得税是:"+tax);
}
}
}
}
class Employee{
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
}