「java代码抽取」java抽奖代码思路

博主:adminadmin 2023-03-20 02:02:08 451

今天给各位分享java代码抽取的知识,其中也会对java抽奖代码思路进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

请问用java从1-33个整数中随机抽取6个数字 且不重复 1-16随机抽取一个数,给小球?

完整代码为:

public class Main {

public static void main(String[] args) {

int index = 1;

int[] redBalls = new int[6];

Random random = new Random();

boolean getMoreRed = true;

boolean getAgain;

System.out.println("开始抽取红球!");

while (getMoreRed) {

getAgain = false;

int red = random.nextInt(36) + 1;

System.out.print("本次抽取到的红球为:[" + red + "]!");

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

if (redBalls[i] == red) {

System.out.print("重复抽取,将重新抽取红球");

getAgain = true;

break;

}

}

System.out.println("");

if (getAgain){

continue;

}

redBalls[index - 1] = red;

index++;

getMoreRed = index 7;

}

System.out.println("抽取到的红球为:");

Arrays.sort(redBalls);

for (int redBall : redBalls) {

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

}

System.out.println("\n\n开始抽取蓝球!");

System.out.println("本次抽取到的蓝球为:[" + (random.nextInt(16) + 1) + "]!");

}

}

运行结果:

普通抽取:

重复时抽取:

Java随机抽取人名完整代码是什么?

public class test {\x0d\x0a public static void main(String[] args) {\x0d\x0a //定义人名数组\x0d\x0a String [] name = {"张三","李四","王五","八神庵","不知火舞","大蛇","景天","唐雪见","李逍遥","赵灵儿"};\x0d\x0a//随机生成数组下标、\x0d\x0a int num = (int)(Math.random() * 1000);\x0d\x0a//对生成的随机数进行判断,如果小于数组下标,就跳出循环\x0d\x0awhile (numname.length-1) {\x0d\x0a if (num

回答于 2022-11-16

Java代码实现抽奖:从班级的学号中抽出一个一等奖,两个二等奖,三个三等奖

抽取问题, 重点是 同一个学号不能重复被抽取.

解决办法很多,

比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取

也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了

参考代码:数组法

import java.util.Random;

public class Test {

public static void main(String[] args) {

int stuNums=30;

int[] nums=new int[stuNums];//存储学号的数组

boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了

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

nums[i]=i+1;//给学号赋值

}

Random r=new Random();

while(true){

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("A等:"+nums[index]);

flags[index]=true; //标记已经被使用过了

break;

}

}

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

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("B等:"+nums[index]);

flags[index]=true;

}else{

i--;//如果已经被抽取过了 ,那么i建议,再次循环

}

}

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

int index = r.nextInt(stuNums);

if(!flags[index]){

System.out.println("c等:"+nums[index]);

flags[index]=true;

}else{

i--;

}

}

}

}

集合法

import java.util.ArrayList;

import java.util.Collections;

public class Test2 {

public static void main(String[] args) {

int stuNums=20;

ArrayListInteger list=new ArrayListInteger();

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

list.add(i+1);

}

System.out.println("有序"+list);

Collections.shuffle(list);//打乱顺序

System.out.println("乱序"+list);

System.out.println("A等"+list.get(0));

System.out.println("B等"+list.get(1));

System.out.println("B等"+list.get(2));

System.out.println("C等"+list.get(3));

System.out.println("C等"+list.get(4));

System.out.println("C等"+list.get(5));

}

}

用java写一段随机抽取3个人的代码

用java.util.Radom类中的方法.

Random rd=new Random();

int persion_id=rd.nextInt(10);//这样可以得到0-10之间的一个随机数(假如总共有十个学生.)

然后用,threeStudent.add(students[stu_id])实现.

有了这个以后,就好说了.循环取得三个学生.送到threeStudent中去.

public void chooseThree()

{

Random rd=new Random();

for(int i=0;i3;i++)

{

threeStudent.add(students[stu_id]);

}

}

祝你好运...

利用java代码抽取CFG能生成什么工具,具有什么功能

这个就要借助hibernate tools跟xdoclet来完成了;

首先你要在你的java代码里应用xdoclet标签,例如

Java code

private String name;

/**

* @hibernate.property column = "name" length = "50"

*/

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

其中,写到javadoc上的@hibernate.property column = "name" length = "50"

就是xdoclet标签,它需要xdoclet程序来处理,这里就需要用到hibernate tools。

具体做的话一般情况是建一个ant脚本来完成,例如:

XML code

target name="hibernate-xdoclet" depends="init, init-xdoclet_hibernate"

description="Generate mapping documents"

echo+---------------------------------------------------+/echo

echo| |/echo

echo| R U N N I N G H I B E R N A T E D O C L E T |/echo

echo| |/echo

echo+---------------------------------------------------+/echo

delete

fileset dir="${hibernate.cfg.xml.dir}" includes="hibernate.cfg.xml" /

/delete

echo message="hibernate.cfg.xml at ${hibernate.cfg.xml.dir}"/echo

sleep seconds="1"/

hibernatedoclet

destdir="${hibernate.cfg.xml.dir}"

excludedtags="@version,@author,@todo,@see"

addedtags="@xdoclet-generated at ${TODAY},@copyright The XDoclet Team,@author XDoclet,@version ${version}"

force="false"

verbose="true"

fileset dir="${src.dir}"

include name="com/**/model/**/*.java"/

/fileset

hibernatecfg

version="3.0"

destDir="${hibernate.cfg.xml.dir}"

dialect="org.hibernate.dialect.Oracle9Dialect"

driver="oracle.jdbc.driver.OracleDriver"

jdbcUrl="jdbc:oracle:thin:@localhost:1521:RESDL"

userName="test"

password="123"

showSql="true"

schema="true"

validateXML="true"

/

hibernate version="3.0"/

/hibernatedoclet

/target

上面的代码是生成hbm跟cfg文件的,下面再介绍如何从java类到数据库:

XML code

target name="hibernate-schema" depends="init, init-hibernate-schema"

description="Generate DB schema from the O/R mapping files"

echo+---------------------------------------------------+/echo

echo| |/echo

echo| R U N N I N G D B S C H E M A |/echo

echo| |/echo

echo+---------------------------------------------------+/echo

echo message="mysql.sql at etc/hbm2doc"/echo

sleep seconds="1"/

hibernatetool destdir="etc/hbm2doc"

configuration propertyFile="${src.dir}/hibernate.properties"

fileset dir="${hibernate.cfg.xml.dir}"

include name="com/**/model/**/*.hbm.xml"/

/fileset

/configuration

hbm2ddl drop="true"

outputfilename="mysql.sql"/

hbm2doc/

/hibernatetool

/target

关于java代码抽取和java抽奖代码思路的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。