"Hundred Days Hundred Questions · Basics" Prepare for the interview, insist on brushing up the questions Chapter 12 - Exception handling and collections!

This column "Hundred Days and Hundred Questions" has been updated for a long time, join this question brushing plan and grow together!


foreword

How to solve the problem?

Some students like to come up and just do it, and come up with the ultimate difficulty topic, thinking that as long as they do the most difficult one, the rest will be solved easily. This kind of eagerness for quick success is unacceptable.

Algorithm training is a systematic project that needs to be done. 循序渐进If you are too eager for quick success, it is easy to feel frustrated because you can't solve the problem and bring counterproductive results.

I remember a colleague of mine did something similar once. I just heard about Niuke.com , so I wanted to go up and try it. After he went up, he chose a difficult topic among the difficult ones, but he couldn't solve it after thinking about it for a long time, which made me very frustrated.

You will find that this approach is very inefficient. Even if that problem is solved, it does not mean that other problems can be solved.

The logical approach is to proceed gradually.

My suggestion is to go through the basic grammar questions first, lay a good foundation and then go further with the algorithm questions, so that first of all, there is absolutely no problem with the basic grammar and common APIs of this language, and it will definitely get twice the result with half the effort when doing algorithm questions later!

This column will take you from basic grammar to advanced algorithms, step by step and continuous practice, join the problem solving plan and work hard together!


For brushing questions here, I recommend Niuke.com. If you are more confident in the basic grammar of the corresponding language, you can also do algorithmic questions. Here, Niuke.com is also very good at classifying the difficulty of algorithmic questions. Brush questions, interview high-frequency questions, etc. Don’t worry if your foundation is poor. Niuke.com also has basic grammar questions to help you learn the basics better.

For the convenience of brushing the questions, I directly put the link of Niuke.com below, and you can click on the blue font to jump directly to brush the questions!

Portal: Entrance to Brush Questions

insert image description here

The column article will take you from basic grammar to advanced algorithms, step by step and continuous practice, join the problem solving plan and work hard together!


JAVA37 Judging students' grades [Exception handling]

Topic:
insert image description here
Solution:

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        try{
    
    
            if(score >= 0 && score <= 100) //正常分数输出
                System.out.println(score);
            else
                throw new ScoreException("分数不合法"); //抛出异常
        }
        catch(ScoreException str){
    
    
            System.out.println(str.getMessage()); //输出异常
        }
    }
}

class ScoreException extends Exception{
    
     //继承自异常类的分数异常处理类
    public ScoreException(String message){
    
     //构造函数
        super(message); //输入异常信息
    }
}

JAVA38 string deduplication [collection class]

topic:
insert image description here

answer:

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        scanner.close();
        HashSet<Character> hs = new HashSet<>();
        for(int i=0;i<str.length();i++){
    
    
            hs.add(str.charAt(i));
        for (char c:hs) {
    
    
            System.out.print(c);
        }
    }
}

JAVA39 collection traversal [collection class]

Topic:
insert image description here
Solution:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int num1 = scanner.nextInt();
        int num2 = scanner.nextInt();
        int num3 = scanner.nextInt();
        int num4 = scanner.nextInt();
        int num5 = scanner.nextInt();
        scanner.close();
        list.add(num1);
        list.add(num2);
        list.add(num3);
        list.add(num4);
        list.add(num5);
        System.out.print("普通for循环:");

        for(int i = 0; i < list.size(); i++){
    
    
            System.out.print(list.get(i) + " ");
        }

        System.out.println();
        System.out.print("增强for循环:");
        
        list.forEach(n -> {
    
    
            System.out.print(n + " ");
        });

        System.out.println();
        System.out.print("迭代器遍历:");
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()){
    
    
            System.out.print(it.next() + " ");
        }

        System.out.println();
    }
}

epilogue

For the convenience of brushing the questions, I directly put the link of the website for brushing the questions below, and you can click on the blue font to jump directly to the questions!

Portal: Artifact for brushing questions

Everyone must be serious about brushing the questions, and don't slack off!

Accumulate over time and become a master!

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/127060651