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源代码
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import oracle.jdbc.driver.OracleDriver;
public class GenPaperServlet extends HttpServlet
{
Connection conn;
Statement stmt;
ResultSet rs;
int total_question_num;
int total_question_in_paper;
int total_paper_num;
String curr_classid;
public GenPaperServlet()
{
conn = null;
stmt = null;
rs = null;
total_question_num = 0;
total_question_in_paper = 0;
total_paper_num = 0;
curr_classid = "";
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
httpservletresponse.setContentType("text/html;charset=GBK");
PrintWriter printwriter = httpservletresponse.getWriter();
printwriter.println("htmlhead/headbodycenter");
printwriter.println("请以POST方式提交");
printwriter.println("/center/body/html");
printwriter.close();
(java)题库中有20道题,随机抽10道不同的题出来 怎么写代码 在线等啊
import java.util.*;
class Untitled {
public static void main(String[] args) throws Exception {
Integer[] questions = generateQuestions();
for(Integer question : questions) {
System.out.println(question);
}
}
private static Integer[] generateQuestions() {
ListInteger questionNumbers = new ArrayListInteger();
Random rand = new Random();
int questionNumber, i = 0;
while(i 10) {
questionNumber = rand.nextInt(21);
if(!questionNumbers.contains(questionNumber)) {
questionNumbers.add(questionNumber);
i ++;
}
}
return questionNumbers.toArray(new Integer[10]);
}
}
java随机抽号程序
现在的可以了,你试下
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class RandomTest extends JFrame {
private static final long serialVersionUID = 6764423443555437772L;
boolean isStop = false;
private JLabel jl = new JLabel();
private JTextField jt = new JTextField();
private JButton start = new JButton("start");
private JButton stop = new JButton("stop");
private JPanel jp = new JPanel();
public RandomTest() {
this.jl.setHorizontalAlignment(SwingConstants.CENTER);
this.jl.setText("60"); // 设置随机数范围1到60
this.jl.setForeground(Color.red); // 设置字体颜色
this.jl.setFont(new Font("Dialog", 1, 24)); // 设置字体样式、大小
GridLayout layout = new GridLayout(1, 2);
this.add(jl, BorderLayout.NORTH);
this.add(jt, BorderLayout.CENTER);
this.jp.add(start, layout);
this.jp.add(stop, layout);
this.add(jp, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭窗口
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isStop = false;
JTextThread jtt = new JTextThread(jt);
jtt.start();
}
});
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isStop = true;
}
});
}
public static void main(String[] args) {
RandomTest rt = new RandomTest();
rt.setSize(400, 300);
rt.setLocationRelativeTo(null); // 设置窗口初始位置,屏幕中央
rt.setVisible(true);
}
class JTextThread extends Thread {
private JTextField jt;
public JTextThread(JTextField jt) {
this.jt = jt;
}
@Override
public void run() {
while (!isStop) {
try {
Thread.sleep(100);
int random = (int) (Math.random() * 60 + 1);
jt.setText(String.valueOf(random));
jt.setForeground(Color.blue); // 设置字体颜色
jt.setFont(new Font("Dialog", 1, 48)); // 设置字体样式、大小
jt.setHorizontalAlignment(SwingConstants.CENTER);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
当前标题:java随机抽题算法代码 java实现抽奖概率算法
文章路径:http://scyanting.com/article/doodedi.html