实训09.09:简单的彩票系统(机选多注)
生活随笔
收集整理的這篇文章主要介紹了
实训09.09:简单的彩票系统(机选多注)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package wsq;
import java.util.Random;
import java.util.Scanner;//機選多注
public class MoreCaiPiao {public static void main(String[] args) {// 定義二維數組 存儲多注彩票int[][] numArray = new int[5][7];/** 二維數組中 未賦值之前的元素值都為0 { {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, .... }*/Scanner scanner = new Scanner(System.in);System.out.println("請輸入機選的注數(最多5注):");int count = scanner.nextInt();// 根據輸入的數字 決定循環的次數for (int k = 0; k < count; k++) {int[] number = new int[7];Random random = new Random();for (int i = 0; i < 6; i++) {// 存儲隨機數是否在數組中出現過boolean isExit = false;// 獲取一個隨機數int temp = random.nextInt(33) + 1;// 隨機的數字不能字節放進數組中,要檢測 temp值是否跟數組中的值沖突// 使用for循環 遍歷number數組,拿出每一個值跟 隨機值對比for (int j = 0; j < 6; j++) {if (temp == number[j]) {// 說明隨機的值 跟數組中某一個值一致isExit = true;// 一旦數字存在 就不需要再往下遍歷了break;// 結束循環}}// 數組遍歷結束 查看isExit的值是否為trueif (isExit) {// 為true 說明隨機數沖突 此次循環是無效的i--;} else {// 為false 說明隨機數沒有出現過number[i] = temp;}}// 藍色的球 第7個number[6] = random.nextInt(16) + 1;for (int i = 0; i < 5; i++) {if (numArray[i][0] == 0) {for (int j = 0; j < 7; j++) {numArray[i][j] = number[j];}// 數據放置結束break;}}}/// -------------------------// 遍歷二維數組 查看值是否設置成功for (int i = 0; i < count; i++) {for (int j = 0; j < 7; j++) {System.out.print(numArray[i][j] + " ");}System.out.println();}}
}
總結
以上是生活随笔為你收集整理的实训09.09:简单的彩票系统(机选多注)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL的环境变量配置详细步骤
- 下一篇: 实训09.09:简单的彩票系统(自选多注