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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

模拟彩票摇号的小游戏(31选7)

發布時間:2024/3/24 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模拟彩票摇号的小游戏(31选7) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

規則:機選7個數
?? ??? ?要求:7個數不能重復;7個數的取值區間1~31之間;7個數都是隨機生成的
?? ?????? 用戶選7個數
?? ??? ?要求:7個數不能重復;7個數的取值區間1~31之間; 7個數都是用戶輸入的
比對機選的數字與用戶選擇的數字是否一致?? ?
思路:
?? ?1,生成7個機選數字,使用數組存儲
?? ?2,讓用戶輸入7個數,使用數組存儲
?? ?3,比對機選的數字與用戶選擇的數字是否一致

方法一:將數據存儲 在數組中,數組長度大小固定

import java.util.Random; import java.util.Scanner; public class Demo01 {public static void main(String[] args) {System.out.println("歡迎來到31選7XX游戲");while(true) {System.out.println();//用戶號碼int[] userNums = user();System.out.print("本次選擇的號碼為:");for (int i : userNums) {System.out.print(i+",");}//中獎號碼int[] randomNums = suiJi();System.out.println();System.out.print("本期中獎號碼為:");for (int i : randomNums) {System.out.print(i+",");}System.out.println();int level = level(randomNums, userNums);switch (level) {case 7:System.out.println("一等獎,別墅靠海");break;case 6:System.out.println("二等獎,500萬");break;case 5:System.out.println("三等獎,心系天下");break;case 4:System.out.println("四等獎,吃火鍋");break;case 3:System.out.println("五等獎,泡面一桶");break;default:System.out.println("謝謝惠顧,感謝您為福利事業做出貢獻,本次中了"+level+"數");break;}}}//機選7個數public static int[] suiJi() {Random ran = new Random();//生成7個機選數字,使用數組存儲int[] num1 = new int[7];for (int i = 0; i < num1.length; i++) {//7個數的取值區間1~31之間int x = ran.nextInt(31)+1;if (compare(num1, x)) {i--;}else {num1[i] = x;}}return num1;}//用戶選7個數public static int[] user() {Random ran = new Random(); Scanner scan = new Scanner(System.in);System.out.println("請輸入1~31之間的數字:");//讓用戶輸入7個數,使用數組存儲int[] num2 = new int[7];for (int i = 0; i < num2.length; i++) {//7個數的取值區間1~31之間System.out.println("請輸入第"+(i+1)+"位數為:");int x = scan.nextInt();//判斷數字是否在1~31范圍內if (x < 1 || x > 31) {System.out.println("請輸入1~31之間的數");i--;//判斷輸入的數字是否重復}else if (compare(num2, x)) {System.out.println("已經選擇過該號碼,請重新選擇");i--;}else {num2[i] = x; }}return num2;}//判斷輸入的是否重復public static boolean compare(int[] num,int tag) {for (int i : num) {if (i == tag) {System.out.println("該數已經存在了!");return true;}}return false;}//計數猜對數的個數public static int level(int[] num1,int[] num2) {int num =0;for (int i : num1) {for (int j : num2) {if (i == j) {num++;}} }return num;} }

?結果 1:

方法二:使用 ArrayList 創建對象 儲存輸入的數據

?

package com.day02.caipiao; import java.util.ArrayList; public class Test {public static void main(String[] args) {System.out.println("******** 歡 迎 來 到 福 利 彩 票 *********");ArrayList<Integer> num1 = QuShu.SuiJi();ArrayList<Integer> num2 = QuShu.UserGuess();System.out.println("本期中獎號碼為:"+num1);System.out.println("用戶選取號碼為:"+num2);int tag = QuShu.Compare(num1, num2);switch (tag) {case 7:System.out.println("一等獎:海邊別墅");break;case 6:System.out.println("二等獎:法拉利一輛");break;case 5:System.out.println("一等獎:喜得500萬");break;case 4:System.out.println("一等獎:獎勵2000¥");break;case 3:System.out.println("一等獎:五元一桶泡面");break;default:System.out.println("感謝您為福利彩票做出的貢獻!");break;}System.out.println("您本次猜到"+tag+"個數,再接再厲!");} }//另創建一個類,獲取數據并處理 package com.day02.caipiao; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.Scanner;public class QuShu {//系統隨機取數public static ArrayList<Integer> SuiJi() {ArrayList<Integer> list = new ArrayList<Integer>();Random random = new Random();while (list.size() < 7) {int num = random.nextInt(31)+1;//判斷隨機生成的數是否存在集合中,不存在就增加if (!list.contains(num)) {list.add(num);}}return list; }//用戶輸入數值public static ArrayList<Integer> UserGuess() {ArrayList<Integer> list2 = new ArrayList<Integer>();Scanner scanner = new Scanner(System.in);while(list2.size() < 7) {System.out.println("請輸入第"+(list2.size()+1)+"個數:");int num1 =scanner.nextInt();if (num1 < 0 ||num1 >31) {System.out.println("此數不在選取范圍內,請重新輸入:");}else if(list2.contains(num1)) {System.out.println("此數已存在,請重新輸入:");}else{list2.add(num1);}}return list2;}//比較用戶輸入的與隨機產生的數值public static int Compare(ArrayList<Integer> list,ArrayList<Integer> list2) {int count = 0;for (Integer integer : list2) {if (list.contains(integer)) {count++;}}return count ;} }

結果 2:

?

?

總結

以上是生活随笔為你收集整理的模拟彩票摇号的小游戏(31选7)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。