Java from entry to master chapter exercises - Chapter 12

Exercise 1 positive and negative output of 26 English letters

Comprehensive exercise 1: The positive and negative output of 26 English letters uses arrays and the ArrayList class, first output A→Z, and then output z→a.

package org.hj.chapter12;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HuJun
 */
public class EnglishLetters {
    
    

    /**
     * 综合练习1:26个英文字母的正反输出 使用数组和ArrayList类,先输出A→Z,再输出z→a。
     */

    public static void main(String[] args) {
    
    
        List<Character> letters = new ArrayList<>();
        //循环加入 26 个字母
        for (char i = 'a'; i <= 'z'; i++) {
    
    
            letters.add(i);
        }

        //输出A→Z,需要转成大写字母
        ArrayList<Character> newLetters = new ArrayList<>();
        for (char i = 0; i < 26; i++) {
    
    
            //利用字符转换大写方法
            newLetters.add(Character.toUpperCase(letters.get(i)));
        }
        System.out.println(newLetters);

        //输出z→a
        List<Character> collect = letters.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(collect);
    }
}

Exercise 2 Demo Account Deposits and Withdrawals

Comprehensive exercise 2: demo account deposit and withdrawal Use the ArrayList class to deposit and withdraw from a demo account, and the running results are shown in the figure below.
insert image description here

package org.hj.chapter12;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HuJun
 */
public class DepositAndWithdrawalFunds {
    
    

    /**
     * 综合练习2:模拟账户存取款 使用ArrayList类模拟账户存取款
     */

    public static void main(String[] args) {
    
    

        //保存余额的List
        List<String> balance = new ArrayList<>();
        balance.add("2015-05-06\t\t\t2000\t\t0\t\t2000");
        balance.add("2015-05-18\t\t\t0\t\t1000\t\t1000");
        balance.add("2015-06-08\t\t\t5000\t\t0\t\t6000");
        balance.add("2015-06-23\t\t\t0\t\t1500\t\t4500");
        balance.add("2015-07-03\t\t\t3000\t\t0\t\t7500");
        balance.add("2015-07-19\t\t\t0\t\t1000\t\t6500");
        balance.add("2015-08-01\t\t\t1000\t\t0\t\t7500");
        balance.add("2015-08-10\t\t\t0\t\t1000\t\t6500");
        System.out.println("存、取款时间\t\t\t存入\t\t支出\t\t余额");
        for (String s : balance) {
    
    
            System.out.println(s);
        }
    }
}

Exercise 3 Sort a random array

Comprehensive exercise 3: Sorting a random array A random array is to assign a value to each element with a random number in an array of a specified length. This is often used in an environment that requires uncertain values, such as a jigsaw puzzle game that requires a random array to disrupt the order of pictures. However, there is also a problem at the same time, that is, the repetition of random numbers. This problem is often ignored. Please use the TreeSet collection to realize the non-repeating sequence, and automatically complete the sorting of elements, and then generate an array. The effect is shown in the figure below.
insert image description here

package org.hj.chapter12;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author HuJun
 */
public class SortRandomArray {
    
    

    public static void main(String[] args) {
    
    

        Set<Integer> set = new TreeSet<>();
        for (int i = 0; i < 10; i++) {
    
    
            //随机生成0-100的整数
            int v = (int) (Math.random() * 100 + 1);
            set.add(v);
        }
        //随机生成的集合
        System.out.println("生成无重复元素且升序排列的随机数组如下:");
        System.out.println(set);
    }
}

Exercise 4 Finding Liangshan Heroes

Comprehensive exercise 4: Looking for heroes in Liangshan Output the nicknames and names of the top ten heroes in Liangshan in "Water Margin" according to the format (such as "Hu Baoyi Song Jiang", "Zhi Duo Xing Wu Yong", etc.). When the nickname of a Liangshan hero (such as "Zhi Duoxing") is input on the console, the console will output the name of the Liangshan hero.

package org.hj.chapter12;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @author HuJun
 */
public class Find108StarsOfDestiny {
    
    

    public static void main(String[] args) {
    
    

        HashMap<String, String> map = new HashMap<>();
        map.put("呼保义", "宋江");
        map.put("玉麒麟", "卢俊义");
        map.put("智多星", "吴用");
        map.put("入云龙", "公孙胜");
        map.put("豹子头", "林冲");
        map.put("霹雳火", "秦明");
        map.put("双鞭", "呼延灼");
        map.put("小李广", "花荣");
        map.put("小旋风", "柴进");
        map.put("扑天雕", "李应");

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一位梁山好汉绰号:");
        String s = scanner.nextLine();
        System.out.println(map.get(s));
    }
}

Exercise 5 play dice

Comprehensive exercise 5: Play dice Zhang San, Li Si, Wang Wu, and Zhao Liu play dice games, compare the points (reminder: add the points of the dice to the ArrayList collection, add the name (key) and the points of the dice to the Map collection ( value), where value is an element in a random ArrayList collection. If the value value is repeated, start over; if the value value is not repeated, output the name of the person with the largest number of points).

package org.hj.chapter12;

import java.util.*;

/**
 * @author HuJun
 */
public class Dice {
    
    

    public static void main(String[] args) {
    
    

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        Map<String, Integer> map = new HashMap<>();
        map.put("张三", list.get((int) (Math.random() * 5) + 1));
        map.put("李四", list.get((int) (Math.random() * 5) + 1));
        map.put("王五", list.get((int) (Math.random() * 5) + 1));
        map.put("赵六", list.get((int) (Math.random() * 5) + 1));


        List<String> duplicateValues = new ArrayList<>();
        List<Integer> values = new ArrayList<>(map.values());

        for (int i = 0; i < values.size(); i++) {
    
    
            for (int j = i + 1; j < values.size(); j++) {
    
    
                if (values.get(i).equals(values.get(j))) {
    
    
                    duplicateValues.add(map.keySet().toArray()[i].toString());
                    duplicateValues.add(map.keySet().toArray()[j].toString());
                }
            }
        }
        if (!duplicateValues.isEmpty()) {
    
    
            System.out.println("出现相同点数,重新投掷");
            return;
        }

        Optional<Integer> first = map.values().stream().sorted(Comparator.reverseOrder()).findFirst();
        String name;
        if (first.isPresent()) {
    
    
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
    
    
                if (entry.getValue().equals(first.get())) {
    
    
                    name = entry.getKey();
                    System.out.println("最大点数:" + name + ":" + first);
                }
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/dedede001/article/details/130338750