成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java隨機選號代碼 java選取隨機數(shù)

java 算法 雙色球

每一步都做成了一個單獨的方法來做,

站在用戶的角度思考問題,與客戶深入溝通,找到柯坪網(wǎng)站設(shè)計與柯坪網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋柯坪地區(qū)。

所以稍顯復(fù)雜,不過注釋都寫了。

完成

1.產(chǎn)生7個隨機數(shù)

2.驗證是否存在相同隨機數(shù)

3.確定號碼選擇區(qū)域

4.排序

//package com.color.util;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class DoubleBall {

//用來保存產(chǎn)生的每注雙色球號碼

private Listint[] ballList;

//保存一注號碼的數(shù)組

private int[] ball;

//紅色球號碼總和的開始區(qū)域

private int START;

//紅色球號碼總和的結(jié)束區(qū)域

private int END;

/**

* 構(gòu)造方法

* @param number,產(chǎn)生號碼的數(shù)量

*/

public DoubleBall(){

Scanner s = new Scanner(System.in);

System.out.println("===請輸入產(chǎn)生隨機號碼數(shù)量===");

int number = s.nextInt();

this.init();

//完成ballList的初始化

ballList = new ArrayListint[]();

for(int i=0;inumber;i++){

//初始化ball

ball = new int[7];

//產(chǎn)生一注號碼

ball = this.createBall(ball);

while(true){

int count = 0;

//將所有的號碼去掉最后一個號碼后相加,用來驗證是否在指定的區(qū)域內(nèi)

for(int j=0;jball.length-1;j++){

count += ball[j];

}

//如果產(chǎn)生的號碼不在指定的區(qū)域內(nèi),重新產(chǎn)生號碼

if(!(countSTART count END)){

ball = this.createBall(ball);

//否則,退出while,將ball添加到list之中

}else{

break;

}

}

ball = this.sort(ball);

//將這注產(chǎn)生的號碼添加到list之中

ballList.add(ball);

}

}

/**

* 產(chǎn)生雙色球的七個號碼

* @param ball

* @return

*/

public int[] createBall(int[] ball){

for(int i=0;iball.length;i++){

//默認(rèn)最后一個號碼保留給藍(lán)色球

if(iball.length-1){

//如果是紅色球,則驗證每個產(chǎn)生的號碼是否已經(jīng)存在。

ball = this.validateBall(ball,i);

}else{

//如果是藍(lán)色球,直接產(chǎn)生1--16之間的隨機數(shù)

ball[i] = (int) Math.round(Math.random()*15+1);

}

}

//產(chǎn)生號碼完畢,返回這注產(chǎn)生的號碼。

return ball;

}

/**

* 每次只產(chǎn)生一個紅色號碼,并驗證此號碼是否已經(jīng)存在,

* 如果存在,重新產(chǎn)生號碼,只到?jīng)]有重復(fù)號碼。

* @param ball

* @param index 產(chǎn)生紅色球數(shù)組的下標(biāo),即第幾個號碼。

* @return

*/

public int[] validateBall(int[] ball,int index){

//產(chǎn)生一個1--33的隨機數(shù)

int random = (int) Math.round(Math.random()*32+1);

while(true){

int i=0;

for(;iindex;i++){

//如果存在重復(fù)數(shù)字

if(random==ball[i]){

//重新產(chǎn)生號碼

random = (int) Math.round(Math.random()*32+1);

//并且跳出for循環(huán),進入while循環(huán)

i =index+1;

}

}

//如果驗證完了所有號碼,那把這個號碼插入數(shù)組,并返回

if(i==index){

ball[index] = random;

return ball;

}

}

}

/**

* 顯示產(chǎn)生的雙色球號碼

*

*/

public void displayBall(){

System.out.println("====產(chǎn)生號碼的詳細(xì)信息如下===");

for(int[] ball:ballList){

for(int i=0;iball.length;i++){

//如果號碼不足兩位數(shù),在前面添加"0"顯示。

System.out.print(((ball[i]10)?("0"+ball[i]):ball[i])+"\t");

}

System.out.println();

}

}

/**

* 完成初始化,制定雙色球號碼的

* 開始和結(jié)束區(qū)域

*

*/

public void init(){

Scanner s = new Scanner(System.in);

System.out.println("===請輸入開始數(shù)字===");

this.START = s.nextInt();

System.out.println("===請輸入結(jié)束數(shù)字===");

this.END = s.nextInt();

if(this.START=25 || this.END=150){

System.out.println("輸入數(shù)字不合法,退出程序!");

System.exit(0);

}

}

/**

* 對號碼進行排序,只對最后一個號碼以前的

* 號碼進行排序

* @param ball

* @return

*/

private int[] sort(int[] ball){

for(int i=0;iball.length-1;i++){

for(int j=0;jball.length-i-2;j++){

if(ball[j]ball[j+1]){

int t = ball[j];

ball[j] = ball[j+1];

ball[j+1] = t;

}

}

}

return ball;

}

public static void main(String[] args){

DoubleBall ball = new DoubleBall();

ball.displayBall();

}

}

用java寫一個抽獎程序

你是要JAVA可視化的 還是控制臺的???

控制臺的話 你可以用循環(huán)來取中獎號,將其存入list或set里(最好是set,因為set本來就不會重復(fù))。如果用list存就另外寫一個循環(huán)方法,來挨個判定該元素是不是已經(jīng)在list中存在了,如果存在,就讓選號的再加選一次,如果不重復(fù),就放到list里去。。。。OK

那你有沒有學(xué)過一種方法,比如說:0到9數(shù)字,隨機選出一個數(shù)字,我??

package suanFa;

import java.util.Random;

public class Shengchengsuijishu {

public static void main(String[] args) {

Random random = new Random(); // 隨機數(shù)類

int number = random.nextInt(10); // 隨機生成 0 - 9 的數(shù)字

int userNumber = 0; // 存儲用戶號碼的變量

boolean sign = false; // 是否循環(huán)的標(biāo)記

while (sign == false) { // 循環(huán)

userNumber = random.nextInt(1000); // 隨機生成 0 - 999 的數(shù)字

sign = checkUserNum(userNumber , number); // 調(diào)用方法

}

}

/**

* 檢查用戶是否中獎

* @param userNumber 隨機生成的用戶號碼

* @param number 開獎號碼

* @return 返回是或否

*/

public static boolean checkUserNum(int userNumber , int number) {

boolean sign = false; // 是否中獎標(biāo)記

int firstNum; // 聲明一個變量記錄百位

int nextNum; // 聲明一個變量記錄十位

String buff = new Integer(userNumber).toString(); // 把 用戶號碼轉(zhuǎn)換成字符串

if (buff.length() == 3) { // 判斷生成的用戶號碼是否 大于 100

firstNum = Integer.parseInt(buff.substring(0, 1)); // 截取百位

nextNum = Integer.parseInt(buff.substring(1, 2)); // 截取十位

if(firstNum - nextNum == number){ // 如果 百位減十位等于開獎號碼,則中獎

System.out.println("開獎號碼是:" + number);

System.out.println("中獎用戶號碼是:" + buff);

sign = true;

}

}

return sign; // 返回

}

☆這是產(chǎn)生0-9的隨機數(shù)的方法:

public class Test{

public static void main(String[] args) {

Random random = new Random(); // 隨機數(shù)類

int number = random.nextInt(10); // 隨機生成 0 - 9 的數(shù)字

System.out.print(number);

}

}

用JAVA編寫一個福利彩票機選模擬器,使用隨機數(shù)方法

自己寫的,不懂可以問我

import java.util.Random;

import java.awt.*;

import java.awt.event.*;

public class shuangSeQiu implements ActionListener{

int i,k;

static int rm,rr;

int [] a = new int [33];

Frame f = new Frame("雙色球隨機數(shù)");

TextField tf = new TextField();

Random rd = new Random();

public shuangSeQiu()

{

for(i=0;i33;i++)

{

a[i] = i;

}

f.setLayout(new BorderLayout());

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void init()

{

tf.addActionListener(this);

Button b = new Button("開始");

b.addActionListener(this);

f.add(tf,"North");

f.add(b);

f.setSize(300,300);

f.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

if(e.getActionCommand().equals("開始"))

//tf.setText(""+(rd.nextInt(33)+1)+" "+(rd.nextInt(33)+1)+" "+(rd.nextInt(33)+1)+" "+(rd.nextInt(33)+1)+" "+(rd.nextInt(33)+1)+" "+(rd.nextInt(33)+1)+" 藍(lán)色球號碼:"+(rd.nextInt(15)+1));

{

/*rr = new Random().nextInt(33);

rm = new Random().nextInt(33);*/

for(i=0;i33;i++)

{

rr = new Random().nextInt(33);

rm = new Random().nextInt(33);

System.out.print(rr+" ");

k=a[rm];

a[rm]=a[rr];

a[rr]=k;

}

tf.setText("紅色球號碼:"+(a[0]+1)+" "+(a[1]+1)+" "+(a[2]+1)+" "+(a[3]+1)+" "+(a[4]+1)+" "+(a[5]+1)+" 藍(lán)色球號碼:"+(rd.nextInt(15)+1));

}

}

public static void main(String[] args)//throws Exception

{

new shuangSeQiu().init();

}

}

當(dāng)前名稱:java隨機選號代碼 java選取隨機數(shù)
文章出自:http://jinyejixie.com/article48/dopcpep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、小程序開發(fā)網(wǎng)站設(shè)計公司、域名注冊用戶體驗、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
安国市| 河西区| 穆棱市| 获嘉县| 来凤县| 云浮市| 佛坪县| 兴义市| 中阳县| 锦屏县| 恭城| 平武县| 平昌县| 玉山县| 岳西县| 拜城县| 西平县| 吴旗县| 高邑县| 固镇县| 荥经县| 塔城市| 汾西县| 赣榆县| 上杭县| 武宁县| 舟山市| 萝北县| 延吉市| 梅河口市| 开封市| 利川市| 邳州市| 昌平区| 玛曲县| 昆明市| 信宜市| 垦利县| 周宁县| 安西县| 禹州市|