Java题目练习
⭕题目一: 统计一句话中重复单词的个数
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
}
Set<Map.Entry<Character, Integer>> entrys = map.entrySet();
for (Map.Entry<Character, Integer> entry : entrys) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
💯思路解析
本题目思路是:"使用一个map集合来解,因为map可以存储键值对,字母为键,次数为值即可,不过需要用的LinkedHashMap,因为这个是可以按照顺序输出的"
⭕题目二: map简单应用
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Amy");
map.put(2, "Joe");
map.put(3, "Tom");
map.put(4, "Susan");
//write your code here......
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
System.out.println(" ");
map.put(5,name);
map.remove(4);
map.put(3,"Tommy");
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+":"+value);
}
}
}
💯思路解析
本题目思路是:"按照题目来即可"
⭕题目三: 集合排序
🌟代码演示
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Customer customer1 = new Customer("小明",scanner.nextInt());
Customer customer2 = new Customer("小军",scanner.nextInt());
Customer customer3 = new Customer("小红",scanner.nextInt());
List<Customer> customers = new ArrayList<>();
customers.add(customer1);
customers.add(customer2);
customers.add(customer3);
//write your code here......
Collections.sort(customers);
System.out.println(customers);
}
}
class Customer implements Comparable<Customer>{
private String name;
private int consumption;
public Customer(String name, int consumption) {
this.name = name;
this.consumption = consumption;
}
@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", consumption=" + consumption +
'}';
}
@Override
public int compareTo(Customer o) {
return o.consumption-consumption; //降序
}
}
💯思路解析
本题目思路是:"重写接口的方法,改它们的排序规则即可"
⭕题目四: 判断各类型字符个数
🌟代码演示
//解法1:根据ASCII码来解
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner rd= new Scanner(System.in);
String a=rd.nextLine();
char [] b=a.toCharArray(); //把字符串变成字符数组
int english=0;
int number=0;
int space=0;
int other=0;
for (int i = 0; i < b.length; i++) {
if(b[i]>='a'&&b[i]<='z'){
english++;
}else if(b[i]>='0'&&b[i]<='9'){
number++;
}else if(b[i]==' '){
space++;
}else {
other++;
}
}
System.out.println("英文字母"+english+"数字"+number+"空格"+space+"其他"+other);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//解法2:直接用Character自带的方法进行检测
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......
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[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);
}
}
💯思路解析
本题目思路是:
解法1:"根据ASCII码表来进行if else的判断"
解法2: "直接用Character自带的方法进行检测,Character.isLetter:判断字符是不是英文;Character.isDigit:判断字符是不是数字;Character.isWhitespace:判断字符是不是空格"
⭕题目五: 编写个人所得税计算程序
🌟代码演示
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
//write your code here......
Employee e1 = new Employee("小明",2500);
Employee e2 = new Employee("小军",8000);
Employee e3 = new Employee("小红",100000);
employees.add(e1) ;
employees.add(e2) ;
employees.add(e3) ;
Iterator it = employees.iterator();
while(it.hasNext()){
Employee e4 = (Employee) it.next() ;
double num = e4.getSalary() - 3500.00;
double sax = 0.0 ;
if(num < 0 ){
}else if(num <= 1500){
sax = num * 0.03 - 0 ;
}else if(num <= 4500){
sax = num * 0.1 - 105 ;
}else if(num <= 9000){
sax = num * 0.2 - 555 ;
}else if(num <= 35000){
sax = num * 0.25 - 1005 ;
}else if(num <= 55000){
sax = num * 0.3 - 2755 ;
}else if(num <= 80000){
sax = num * 0.35 - 5505 ;
}else if(num > 80000){
sax = num * 0.45 - 13505 ;
}
System.out.println(e4.getName()+"应该缴纳的个人所得税是:"+sax);
}
}
}
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;
}
}
💯思路解析
本题目思路:"根据题目来即可"
⭕题目六:记录点赞用户
🌟代码演示
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 {
// write your code here......
public HashSet<String> hs = new HashSet();
public void like(String username){
if(hs.contains(username)){
hs.remove(username);
}else{
hs.add(username);
}
}
public String[] getLikeUsers(){
return hs.toArray(new String[hs.size()]);
}
}
💯思路解析
本题目思路:"根据题目来即可"
⭕题目七:回文数判断
🌟代码演示
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Main main = new Main();
int number = console.nextInt();
System.out.println(main.palindromeNumber(number));
}
public Boolean palindromeNumber(int number) {
String numStr = String.valueOf(number);
char numChar[] = numStr.toCharArray();
if (numChar[0]==numChar[4]&&numChar[1]==numChar[3]) {
return true;
}
return false;
}
}
💯思路解析
本题目思路:"根据题目来即可"
⭕题目八:判断素数个数
🌟代码演示
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 is_prime(int num){
for(int i=2;i<num;i++){
if(num%i==0)
return false;
}
return true;
}
public static void method(int start, int end) {
int count=0;
//write your code here......
if(end<start)
start=start+end-(end=start);
for(int i=start;i<=end;i++){
if(i<=2)
continue;
if(is_prime(i))
count++;
}
System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数");
}
}
💯思路解析
本题目思路:"素数又叫质数,指的是在大于1的自然数中,除了1和它本身,没有别的因数,也就是如果一个数只能被1和它本身整除,那么这个数就叫做素数"
⭕题目九: 根据周长求面积
🌟代码演示
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);
}
//2πr = 圆的周长
double r = super.getS() / (2 * Math.PI);
public double getArea(){
return Math.PI * r * r;
}
}
// 方形
class Square extends Shape implements Area {
//write your code here......
public Square(double s){
super(s);
}
//4a = 正方形周长
double side = super.getS() / 4;
public double getArea(){
return side * side;
}
}
💯思路解析
本题目思路是:根据题目描述来即可
⭕题目十: 冒泡排序
🌟代码演示
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[7];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
//write your code here......
Arrays.sort(arr);
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+" ");
}
}
}
💯思路解析
本题目思路是:"这里使用的是直接sort进行排序"
作者:KJ.JK
文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习