今天主要对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); } }
标签:2023.09,java,18,System,static,println,public,out From: https://www.cnblogs.com/dmx-03/p/17715606.html