java抽獎程序,java實現概率抽獎

本文目錄一覽:

用swing編寫一個java抽獎的程序,至少有5個按鈕,顯得高大上一點。

#8205;

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.JOptionPane;

import javax.swing.border.Border;

import javax.swing.border.LineBorder;

public class 抽獎遊戲 extends JFrame {

 static 抽獎遊戲 frame;

 public 抽獎遊戲() {

  setLayout(new GridLayout(3, 3, 3, 3));

  Border border = new LineBorder(Color.BLUE, 4);

  JButton l1 = new JButton();

  JButton l2 = new JButton();

  JButton l3 = new JButton();

  JButton l4 = new JButton();

  JButton l5 = new JButton();

  JButton l6 = new JButton();

  JButton l7 = new JButton(“祝你好運”);

  JButton l8 = new JButton(“中獎規則”);

  JButton l9 = new JButton(“再次抽獎”);

  l1.setBorder(border);

  l2.setBorder(border);

  l3.setBorder(border);

  l4.setBorder(border);

  l5.setBorder(border);

  l6.setBorder(border);

  l7.setBorder(border);

  l8.setBorder(border);

  l9.setBorder(border);

  String[] num2 = { “1”, “2”, “3”, “4”, “5”, “6” };

  l8.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    JOptionPane.showMessageDialog(null, “遊戲規則:\n” + “特等獎:\n”

      + “第一行或者第二行出現的數字全部相同\n”

      + “第一第二行出現升序的1,2,3,4,5,6或者降序的6,5,4,3,2,1\n” + “一等獎\n”

      + “1:第一行或第二行的數字降序排列或者升序排列\n”

      + “2:第一行或者第二行相鄰的按鈕出現一樣的數字\n”);

   }

  });

  l9.addActionListener(new ActionListener() {

   @Override

   public void actionPerformed(ActionEvent e) {

    // TODO Auto-generated method stub

    l1.setText(“*”);

    l2.setText(“*”);

    l3.setText(“*”);

    l4.setText(“*”);

    l5.setText(“*”);

    l6.setText(“*”);

    l7.setBackground(Color.lightGray);

    l7.setText(“離中獎不遠了!”);

   }

  });

  l6.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  l5.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  l4.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  l3.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  l2.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  l1.addActionListener(new ActionListener() {

   public void actionPerformed(ActionEvent arg0) {

    // TODO Auto-generated method stub

    int j1 = (int) (Math.random() * 10) % 6;

    l1.setText(num2[j1]);

    int j2 = (int) (Math.random() * 10) % 6;

    l2.setText(num2[j2]);

    int j3 = (int) (Math.random() * 10) % 6;

    l3.setText(num2[j3]);

    int j4 = (int) (Math.random() * 10) % 6;

    l4.setText(num2[j4]);

    int j5 = (int) (Math.random() * 10) % 6;

    l5.setText(num2[j5]);

    int j6 = (int) (Math.random() * 10) % 6;

    l6.setText(num2[j6]);

    if (j1 == j2  j2 == j3 || j4 == j5  j5 == j6 || j1 == 1

       j2 == 2  j3 == 3  j4 == 4  j5 == 5  j6 == 6

      || j1 == 6  j2 == 5  j3 == 4  j4 == 3  j5 == 2

       j6 == 1) {

     l7.setBackground(Color.red);

     l7.setText(“特等獎”);

    } else if (j1  j2  j2  j3 || j3  j2  j2  j1 || j4  j5

       j5  j6 || j6  j5  j5  j4 || j1 == j2

       j5 == j4 || j1 == j2  j5 == j6 || j3 == j2

       j4 == j5 || j5 == j6  j2 == j3) {

     l7.setBackground(Color.YELLOW);

     l7.setText(“一等獎”);

    } else {

     l7.setBackground(Color.lightGray);

     l7.setText(“未中獎,加油!”);

    }

   }

  });

  Font font = new Font(“”, Font.BOLD, 20);

  l1.setFont(font);

  l2.setFont(font);

  l3.setFont(font);

  l4.setFont(font);

  l5.setFont(font);

  l6.setFont(font);

  l7.setFont(font);

  l8.setFont(font);

  l9.setFont(font);

  add(l1);

  add(l2);

  add(l3);

  add(l4);

  add(l5);

  add(l6);

  add(l7);

  add(l8);

  add(l9);

 }

 public static void main(String[] args) {

  // TODO Auto-generated method stub

  frame = new 抽獎遊戲();

  frame.setTitle(“抽獎大戰”);

  frame.setSize(500, 500);

  frame.setLocationRelativeTo(null);

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }

}

Java用do-while循環寫出一個抽獎程序

//這個是我習慣的包名,呵呵package com.james.wk;import java.util.Random;

import java.util.Scanner;public class guess {

public static boolean getIt() {

Scanner input = new Scanner(System.in);

Random ra = new Random();

System.out.println(“請輸入你的會員號:”);

int a=0; //初始化會員號

a = input.nextInt();

int b = ra.nextInt(10); //隨機數

System.out.println(“幸運數是:” + b);

int c = 0; //定義a的百位數

if (a = 999 || a = 10000) {

System.out.println(“請輸入四位數”);

getIt();

}

c = a / 100 % 10;

if (b == c) {

return true;

}

return false;

} public static void main(String[] args) {

String sure=null;

do{

System.out.println(“是否開始猜獎?(任意鍵開始,』N『下次退出)”);

sure=new Scanner(System.in).next();

if(sure.toLowerCase().equals(“n”)){

System.out.println(“您已經退出!”);

return;

}else{

if (new guess().getIt()) {

System.out.println(“恭喜你是猜到了!”);

} else {

System.out.println(“猜錯了”);

}

}

}while(1==1);

}

}

會有點漏洞,會員號輸入時驗證不能為其他字元沒有做,不好意思

用java寫一個抽獎程序

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

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

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抽獎程序

public String[] findluckMen(String[] data , int n){

Random random = new Random();

int len = data.length;

String[] luckmen = new String[len];

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

int index = random.nextInt(len);

luckmen[i] = data[index ];

}

return luckmen;

}

JAVA抽獎問題

import java.util.Scanner;public class TextRandom {

public static Scanner input=new Scanner(System.in);

public static int count=0;//記錄用戶猜對的次數

public static int xitong =(int)Math.round(Math.random()*10);//系統產生的隨機數0~10之間

public static int w[]=new int[3];//將系統隨即產生的數字放在數組W中

public static int guess[]=new int [3];

public static void main(String[]args)

{

guess();

}

public static void guess()

{ //開始抽獎

for(int j=0;jw.length;j++){

System.out.println(“開始抽獎,請用戶輸入0~10之間的數字,請輸入第”+(j+1)+”個數字”);

guess[j] =input.nextInt();

if(j2){

System.out.println(“正在搖獎,需要中途放棄么 ?(y/n)”);

String shuru =input.next();

if(shuru.equals(‘y’)) break;

}

}

//系通開出的獎

System.out.println(“系統產生三個數字是:”);

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

{

System.out.print((int)Math.round(Math.random()*10));

w[i]=(int)Math.round(Math.random()*10);

}

//兌獎過程

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

{

if(guess[i]==w[i])

{

count++;//記錄出用戶抽中獎的次數

}

}

if(count==3)

{

System.out.println(“恭喜你,你中獎了 “);

}

else

{

System.out.println(“很遺憾,您沒有中獎,歡迎下次再來!”);

}

}

}

//我已儘力了

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/155118.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-17 02:38
下一篇 2024-11-17 02:38

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29

發表回復

登錄後才能評論