日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

小学数学闯关游戏 java代码_Java语言实现小学数学练习

發(fā)布時間:2025/3/21 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小学数学闯关游戏 java代码_Java语言实现小学数学练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

package per.java.shejiti;

import java.io.*;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

public class XiaoXueShuXueJiSuan{

static Scanner sc = new Scanner(System.in);

static int score; // 分?jǐn)?shù)

static long time; // 時間

static int book; // 控制操作不重復(fù)

static String [] achievement = new String[11]; // 成績單

static int index = 0; // 第 index 次成績

public static void main(String[] args) {

System.out.println("請輸入用戶名ID:"); // 輸入用戶名ID

String str = sc.next();

while(!judge(str)) { // 判斷用戶名ID是否正確, 錯誤重新輸入直到用戶名ID正確為止

System.out.print("請重新輸入用戶名ID: ");

str = sc.next();

}

System.out.println("用戶名正確!");

try {

// 此處我放在了這個位置,可以更換

File file = new File("E:\\Java語言\\Java語言代碼\\javaexample//record");

if(!file.isFile()) // 如果沒有該文件,就新建一個

file.createNewFile();

FileWriter fileWriter = new FileWriter(file, true); // 通過 true 設(shè)置追加

fileWriter.write("你以前的記錄是: \n");

fileWriter.flush(); // 清空緩存區(qū),壓入文件

fileWriter.close();

boolean flag = true;

List list = new ArrayList();

for(int i = 0;i < 4;i++) // 初始化數(shù)組,控制前四個運(yùn)算

list.add(i);

while(flag){

System.out.println("請選擇:\n(1)開始測試\n(2)檢查分?jǐn)?shù)\n(3)退出");

int choice = sc.nextInt();

switch(choice) {

case 1:{ // 開始測試

score = 0; // 重置分?jǐn)?shù)為 0

book = list.get(3); // 標(biāo)記上一次運(yùn)算

long start = System.currentTimeMillis(); // 開始時間

Collections.shuffle(list); // 對數(shù)組亂序,保證前四次運(yùn)算亂序

for(int j = 0;j < 10;j++)

if(j < 4) calculate(list.get(j)); // 前四次包括 +-*/ 保證每個至少出現(xiàn)一次

else calculate();

long end = System.currentTimeMillis(); // 結(jié)束時間

time = (end - start) / 1000; // 共用時間

fileWriter = new FileWriter(file, true); // 通過 true 設(shè)置追加

fileWriter.write(str + " " + score + " " + time + " 秒\n");

fileWriter.flush(); // 清空緩存區(qū),壓入文件

fileWriter.close(); // 關(guān)閉寫入流

System.out.println("\n問題 | 正確答案 | 你的答案 ");

for(int i = 0;i < 10;i++)

System.out.println(achievement[i]); // 輸出每次成績

System.out.println();

index = 0; // 重置成績單

break;

}

case 2:{

FileReader fileReader = new FileReader(file);

BufferedReader in = new BufferedReader(fileReader);

String s;

while((s = in.readLine()) != null) // 讀取文件全部內(nèi)容

System.out.println(s);

System.out.println();

fileReader.close();

break;

}

case 3:{

System.out.println("退出成功!");

flag = false;

file.delete(); // 刪除文件

break;

}

}

}

}

catch(Exception e) {

e.printStackTrace();

}

sc.close();

}

//判斷用戶名是否正確方法

public static boolean judge(String str) {

if(str.length() != 6) { // 六位組成

System.out.println("提示: 憨批, 用戶名ID是六位大寫字母和數(shù)字組成的哦! ");

return false;

}

for(int i = 0;i < 2;i++)

if(!Character.isUpperCase(str.charAt(i))) { // 前兩位不是大寫字母

System.out.println("提示: 憨批, 用戶名ID的前兩位應(yīng)該是大寫字母哦! ");

return false;

}

for(int i = 2;i < 6;i++)

if(!Character.isDigit(str.charAt(i))) { // 后四位不是數(shù)字

System.out.println("提示: 憨批, 用戶名ID的后四位應(yīng)該是數(shù)字哦! ");

return false;

}

return true;

}

// 隨機(jī)運(yùn)算操作

public static void calculate() {

int n = (int)(Math.random()*8)%4; // 四種運(yùn)算法,隨機(jī)產(chǎn)生

while(book == n) // 相鄰運(yùn)算不能相同

n = (int)(Math.random()*8)%4;

book = n;

switch(n) {

case 0:add();break;

case 1:subtract();break;

case 2:multiplication();break;

case 3:division();break;

}

}

// 有參數(shù)運(yùn)算操作, 重載方法

public static void calculate(int n) {

switch(n) {

case 0:add();break;

case 1:subtract();break;

case 2:multiplication();break;

case 3:division();break;

}

}

// +

public static void add() {

int num1 = (int)(Math.random()*99 + 1); // 產(chǎn)生 [1, 99] 的整數(shù)

int num2 = (int)(Math.random()*(100-num1-1)+1); // 保證兩數(shù)運(yùn)算結(jié)果小于100, 以下同理

System.out.print(num1 + " + " + num2 + " = ");

int result = sc.nextInt();

achievement[index++] = String.format("%-2d + %-2d = | %-2d | %-2d", num1, num2, num1+num2, result);

if(result == num1+num2)

score += 10; // 得分加 10 分

}

// -

public static void subtract() {

int num1 = (int)(Math.random()*99 + 1);

int num2 = (int)(Math.random()*99 + 1);

while(num1 == num2) // 保證兩數(shù)不相等

num2 = (int)(Math.random()*99 + 1);

if(num1 < num2) { // 用最大值-最小值

int temp = num1;

num1 = num2;

num2 = temp;

}

System.out.print(num1 + " - " + num2 + " = ");

int result = sc.nextInt();

achievement[index++] = String.format("%-2d - %-2d = | %-2d | %-2d", num1, num2, num1-num2, result);

if(Math.abs(Math.abs(num1-num2) - result) < 1e-6) // 精確到10的-6次方算得分

score += 10;

}

// *

public static void multiplication() {

int num1 = (int)(Math.random()*99 + 1); // 產(chǎn)生 [1, 99] 的整數(shù)

int num2 = (int)(Math.random()*(100/num1-1)+1);

System.out.print(num1 + " * " + num2 + " = ");

int result = sc.nextInt();

achievement[index++] = String.format("%-2d * %-2d = | %-2d | %-2d", num1, num2, num1*num2, result);

if(result == num1*num2)

score += 10;

}

// /

public static void division() {

double weighting = Math.random();

int num1, num2;

if(weighting < 50)

num1 = (int)(Math.random()*20 + 1); // 產(chǎn)生 [1, 20] 的整數(shù) 加權(quán) 50%

else if(weighting < 80)

num1 = (int)(Math.random()*30 + 21); // 產(chǎn)生 [21, 50] 的整數(shù) 加權(quán) 30%

else

num1 = (int)(Math.random()*49 + 51); // 產(chǎn)生 [51, 99] 的整數(shù) 加權(quán) 20%

if(num1 > 50) // 如果num1 > 50, num2 只能等于 num1

num2 = num1;

else

num2 = ((int)(Math.random()*(100/num1-1) + 1) )*num1; // num2 必須是 num1 的整數(shù)倍,保證結(jié)果為整數(shù)

System.out.print(num2 + " / " + num1 + " = ");

int result = sc.nextInt();

achievement[index++] = String.format("%-2d / %-2d = | %-2d | %-2d", num2, num1, num2/num1, result);

if(result == num2/num1)

score += 10;

}

}

總結(jié)

以上是生活随笔為你收集整理的小学数学闯关游戏 java代码_Java语言实现小学数学练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。