2023.09.18

今天主要对java的方法和类进行了学习,学习了如何定义方法和类。以及当中的作用

public/private class 类名 {

public/private 数据类型 变量名;

public/private 数据类型 方法名(参数列表){ }

}

public: 存取与访问不受限制; private: 除非是属于类自己的方法,外界代码不能存取和访问;

对于类中的私有变量,可以用set 和get进行调用。

还学习了方法的重载,即

        在同一个类中,方法名相同,参数不同(个数不同,类型不同)

               (方法重载与返回值类型无关,方法重载与形参的名字无关)

还进行了程序的编写

复制代码
package SoreInformation;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.HashSet;

public class Demo {
    static Scanner input = new Scanner(System.in);   
    static HashSet<String> usedQuestions = new HashSet<>();
    static Timer timer = new Timer();
    static int remainingTime = 600; // 10 minutes
    public static void main(String[] args) {
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                remainingTime--;
                if (remainingTime <= 0) {
                    timer.cancel();
                    System.out.println("时间到!游戏结束。");
                    System.exit(0);
                }
            }
        }, 0, 1000);

        int num1;
        int num2;
        int[] wrong = new int[40];
        int c = 0;
        int d = 0;
        int i = 0;
        do {
            int operator = (int) (Math.random() * 4);
            int a = (int) (Math.random() * 20) + 1;
            int b = (int) (Math.random() * 20) + 1;
            String question = a + getOp(operator) + b;
            if (b != 0 && a % b == 0 && a > b && !usedQuestions.contains(question)) {
                i++;
                usedQuestions.add(question);
                System.out.println(i + ":" + question + "=?");
                System.out.println("输入答案:");
                int enterAnswer = input.nextInt();
                int answer = calculation(a, b, operator);
                if (enterAnswer == answer) {
                    System.out.println("恭喜!计算成功");
                    c++;
                } else {
                    System.out.println("计算错误");
                    d++;
                    wrong[d] = i;
                }
            } else {
                continue;
            }
            System.out.println("剩余时间:" + formatTime(remainingTime));
        } while (i < 30);
        System.out.println("正确数量:" + c);
        System.out.println("正确率:" + (c * 100 / 30) + "%");
        System.out.println("错题总数是:" + d);
        System.out.print("错题数是:");
        for (int p = 1; p <= d; p++) {
            System.out.print(wrong[p] + ",");
        }
    }

    public static String getOp(int operator) {
        String string = "";
        switch (operator) {
            case 0:
                string = "+";
                break;
            case 1:
                string = "-";
                break;
            case 2:
                string = "*";
                break;
            case 3:
                string = "/";
                break;
        }
        return string;
    }

    public static int calculation(int a, int b, int operator) {
        int sum = 0;
        switch (operator) {
            case 0:
                sum = a + b;
                break;
            case 1:
                sum = a - b;
                break;
            case 2:
                sum = a * b;
                break;
            case 3:
                sum = a / b;
                break;
        }
        return sum;
    }

    public static String formatTime(int seconds) {
        int minutes = seconds / 60;
        seconds %= 60;
        return String.format("%02d:%02d", minutes, seconds);
    }
}
复制代码

 

posted @   new菜鸟  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示