App下載

使用超簡潔Java代碼生成雙色球若干個注隨機號碼

猿友 2021-07-15 10:35:30 瀏覽數(shù) (2960)
反饋

本文章將為您介紹一個有趣的Java程序代碼,如何使用超簡潔的Java代碼實現(xiàn)雙色球若干注隨機號碼的生成,有興趣的小伙伴可以嘗試一下。

Mavan pom文件引用依賴 

 <!-- hutool工具類-->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.3.6</version>
		</dependency>
    <!-- google java類庫-->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
    </dependency>

 java 單類實現(xiàn)代碼,在編輯器里,粘貼下面的代碼。鼠標右鍵、run運行

import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
 
import java.util.List;
 
/**
 * @Author tarzan
 * @Date 2021/4/1 14:43
 * @Description 模擬雙色球,隨機生成若干住號碼
 */
public class DoubleColorBall {
 
 
  /**
   * 主方法
   * */
  public static void main(String[] args) {
    getDoubleColorBallNumber(5);
  }
 
  /**
   * 獲取多注雙色球號碼
   * */
  public static void getDoubleColorBallNumber(int num){
    System.out.println("隨機生成"+num+"注雙色球號碼為:");
    String resultNumber="";
    for (int i = 0; i < num; i++) {
      System.out.println("【"+(i+1)+"】 "+resultNumber+getDoubleColorBallNumber());
    }
 
  }
 
  /**
   * 獲取單注雙色球號碼
   * */
  public static String getDoubleColorBallNumber(){
    String resultNumber="";
    for (int i = 0; i < 6; i++) {
      String ballNumber= RandomUtil.randomEle(getRedBalls())+"	";
      resultNumber=resultNumber+ballNumber;
    }
    return resultNumber+RandomUtil.randomEle(getBlueBalls());
  }
 
  /**
   * 獲取紅球球號集合
   * */
  public static List<String> getRedBalls(){
    return getBalls(33);
  }
 
  /**
   * 獲取藍球球號集合
   * */
  public static List<String> getBlueBalls(){
    return getBalls(16);
  }
 
  /**
   * 獲取球號集合
   * */
  public static List<String> getBalls(int num){
    List<String> redBalls= Lists.newArrayList();
    for (int i = 1; i <=num; i++) {
      int length=String.valueOf(num).length();
      String str = String.format("%0"+length+"d",i);
      redBalls.add(str);
    }
    return redBalls;
  }
}

結(jié)果圖

結(jié)果輸出

關(guān)于如何使用Java程序代碼實現(xiàn)雙色球若干注隨機號碼的生成就介紹到此結(jié)束了,更多相關(guān)java內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章!


0 人點贊