Java實現(xiàn)擲骰子控制臺和窗體兩種方法
本文實例為大家分享了Java控制臺和窗體實現(xiàn)擲骰子的具體代碼,供大家參考,具體內(nèi)容如下
程序目標:同時3擲骰子,讓骰子轉動若干次后停下來的正面朝上的數(shù)字之和大于9 則為大,小于等于9則為小
用于需要提前選擇押大還是小,程序結束返回是否押中的結果。
1.擲骰子控制臺實現(xiàn)
本程序分為三層:
- 表示層 :用類Player2 實現(xiàn)
- 業(yè)務邏輯層:類DiceGame2 實現(xiàn)
- 數(shù)據(jù)/技術服務層:類Dice2 實現(xiàn)
Dice2 類 實現(xiàn)Runnable接口,重新run()方法來設置每個骰子轉動10次 ,并在停下來后獲取正面朝上的值。
DiceGame2 類中創(chuàng)建三個線程來模擬同時擲3個骰子,并能在在骰子轉動結束后得到總點數(shù)和每個骰子的點數(shù)。
其中roll()方法開啟線程, result()計算點數(shù)
Player2 類 主函數(shù)創(chuàng)建菜單,提示用戶輸入操作。并在控制臺顯示結果。
代碼如下:
public class Player2 { //表示層
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("**************擲 骰 子 游 戲***************");
System.out.println("請輸入0表示押小,輸入1表示押大,輸入2表示退出");
int i = sc.nextInt();
if(i == 2)
System.exit(0);
DiceGame2 d = new DiceGame2();
d.roll();
Thread.sleep(500);// 主線程等待
int n = d.result();
if (i == 0 && n <= 9)
System.out.println("恭喜你,押小贏了");
else if (i == 0 && n > 9)
System.out.println("很遺憾,押小輸了");
else if (i == 1 && n > 9)
System.out.println("恭喜你,押大贏了");
else if (i == 1 && n <= 9)
System.out.println("很遺憾,押大輸了");
System.out.println();
}
}
}
class DiceGame2 { //業(yè)務邏輯層
Dice2 d1, d2, d3;
public DiceGame2() {
}
public void roll() { // 同時三個擲骰子
d1 = new Dice2();
d2 = new Dice2();
d3 = new Dice2();
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
Thread t3 = new Thread(d3);
t1.start();
t2.start();
t3.start();
}
public int result() { // 計算點數(shù)
int sum = 0;
int i = d1.getUpNum();
int j = d2.getUpNum();
int k = d3.getUpNum();
sum = i + j + k;
System.out.println("擲得點數(shù)和為:" + sum + " [" + i + " - " + j + " - " + k + " ]");
return sum;
}//
}
class Dice2 implements Runnable { // 骰子類 數(shù)據(jù)/技術服務層
private int upnum;
public Dice2() {
this.upnum = 1;
}
@Override
public void run() {
int count = 10;// 設置每顆骰子轉動10次
while (count > 0) {
upnum = new Random().nextInt(6) + 1;
System.out.println(Thread.currentThread().getName() + " " + upnum);
count--;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public int getUpNum() {
return upnum;
}
}
2.擲骰子窗體實現(xiàn)
窗體程序思路和控制臺一樣,只是把顯示界面由控制臺改為窗體。
類SiziGame extends JFrame implements ActionListener
類IconThread implements Runnable
首先,SiziGame類通過構造器初始化設置窗體和里面控件,并且把代表6種點數(shù)的圖片加載到imgs集合里面,還要給開始按鈕綁定監(jiān)聽事件函數(shù)。
其次,重寫actionPerformed()函數(shù),來實現(xiàn)按鈕觸發(fā)3個骰子轉動。在這個函數(shù)里開始3個線程讓骰子圖片轉動起來,并在轉動結束后計算機每個骰子顯示圖片對應的點數(shù)和總點數(shù)。再根據(jù)用戶之前選擇的押大或押小返回輸贏結果。
然后,IconThread 類重寫run()方法,來實現(xiàn)圖片的動態(tài)效果。通過隨機生成[0-6)的隨機整數(shù)作為imgs集合的index,再根據(jù)索引從imgs里取對應的圖片顯示在窗體上,由于線程運行速度較快,且設置了多次取圖片的動作從而形成了動態(tài)效果。注意的是集合下標為[0-5],所以在SiziGame類中的result()方法計算點數(shù)時要+1。
public class SiziGame extends JFrame implements ActionListener{
private JLabel lb1;
private JLabel lb2;
private JLabel lb3;
private JLabel lbNote;
private JComboBox<String> cmb;
private JButton btn;
private JLabel labResult;
private static List<Icon> imgs = new ArrayList<Icon>();
IconThread it1,it2,it3;
Thread t1,t2,t3;
public static void main(String[] args) {
new SiziGame();
}
public SiziGame(){
this.setLocationRelativeTo(null);
this.setBounds(200, 50, 380, 297);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
this.setResizable(false);
lb1 = new JLabel("");
lb1.setIcon(new ImageIcon( getClass().getResource("img/a.jpg")));
lb1.setBounds(30, 30, 96, 96);
getContentPane().add(lb1);
lb2 = new JLabel("");
lb2.setIcon(new ImageIcon( getClass().getResource("img/a.jpg")));
lb2.setBounds(136, 30, 96, 96);
getContentPane().add(lb2);
lb3 = new JLabel("");
lb3.setIcon(new ImageIcon( getClass().getResource("img/a.jpg")));
lb3.setBounds(242, 30, 96, 96);
getContentPane().add(lb3);
lbNote = new JLabel("押");
lbNote.setBounds(40, 200, 30, 30);
getContentPane().add(lbNote);
cmb = new JComboBox<String>();
cmb.setBounds(80, 200, 60, 30);
getContentPane().add(cmb);
cmb.addItem("大");
cmb.addItem("小");
btn = new JButton("START");
btn.setBounds(220, 190, 100, 50);
btn.addActionListener(this);
getContentPane().add(btn);
labResult = new JLabel("結果"); //結果
labResult.setBounds(136, 156, 126, 27);
getContentPane().add(labResult);
this.setVisible(true);
imgs.add(new ImageIcon(getClass().getResource("img/1.gif")));
imgs.add(new ImageIcon(getClass().getResource("img/2.gif")));
imgs.add(new ImageIcon(getClass().getResource("img/3.gif")));
imgs.add(new ImageIcon(getClass().getResource("img/4.gif")));
imgs.add(new ImageIcon(getClass().getResource("img/5.gif")));
imgs.add(new ImageIcon(getClass().getResource("img/6.gif")));
it1 = new IconThread(lb1, imgs);
it2 = new IconThread(lb2, imgs);
it3 = new IconThread(lb3, imgs);
t1 = new Thread(it1);
t2 = new Thread(it2);
t3 = new Thread(it3);
}//
public void thread(){
/*IconThread it1 = new IconThread(lb1, imgs);
IconThread it2 = new IconThread(lb2, imgs);
IconThread it3 = new IconThread(lb3, imgs);
Thread t1 = new Thread(it1);
Thread t2 = new Thread(it2);
Thread t3 = new Thread(it3);*/
it1 = new IconThread(lb1, imgs);
it2 = new IconThread(lb2, imgs);
it3 = new IconThread(lb3, imgs);
t1 = new Thread(it1);
t2 = new Thread(it2);
t3 = new Thread(it3);
/* t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {}*/
}
@Override
public void actionPerformed(ActionEvent args) {
String ya = cmb.getSelectedItem().toString();
System.out.println("\n你本次押的"+ya);
thread();
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {}
int i = result(lb1);
int j = result(lb2);
int k = result(lb3);
int sum = i + j + k;
System.out.println("擲得點數(shù)和為:" + sum + " [" + i + " - " + j + " - " + k + " ]");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
if (sum > 9 && "大".equals(ya) || sum <= 9 && "小".equals(ya)) {
labResult.setText("贏");
labResult.setForeground(Color.GREEN);
labResult.setFont(new Font("宋體", Font.BOLD, 30));
}
else {
labResult.setText("輸");
labResult.setForeground(Color.red);
labResult.setFont(new Font("宋體", Font.BOLD, 30));
}
}//
public int result(JLabel lab) {
Icon icon = lab.getIcon();// 獲取當前骰子圖片
int sum = 0;
for (int i = 0; i < imgs.size(); i++) {
if (icon.equals(imgs.get(i))) { //取出和lab最后圖片對應的點數(shù)
sum += (i + 1);
break;
}
}
return sum;
}//
}


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
mybatis批量update時報錯multi-statement not allow的問題
這篇文章主要介紹了mybatis批量update時報錯multi-statement not allow的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Java中StringBuilder字符串類型的操作方法及API整理
Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理2016-05-05
Spring?Cloud?+?Nacos?+?Seata整合過程(分布式事務解決方案)
Seata 是一款開源的分布式事務解決方案,致力于在微服務架構下提供高性能和簡單易用的分布式事務服務,這篇文章主要介紹了Spring?Cloud?+?Nacos?+?Seata整合過程(分布式事務解決方案),需要的朋友可以參考下2022-03-03
詳解Java如何實現(xiàn)百萬數(shù)據(jù)excel導出功能
這篇文章主要為大家詳細介紹了Java如何實現(xiàn)百萬數(shù)據(jù)excel導出功能,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-02-02
Mybatis中 mapper-locations和@MapperScan的作用
這篇文章主要介紹了Mybatis中 mapper-locations和@MapperScan的作用,mybatis.mapper-locations在SpringBoot配置文件中使用,作用是掃描Mapper接口對應的XML文件,需要的朋友可以參考下2023-05-05
SpringCloud Feign傳遞HttpServletRequest對象流程
HttpServletRequest接口的對象代表客戶端的請求,當客戶端通過HTTP協(xié)議訪問Tomcat服務器時,HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學可以參考下文2023-05-05

