亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)擲骰子控制臺和窗體兩種方法

 更新時間:2021年08月10日 17:07:50   作者:sinadrew  
這篇文章主要為大家詳細介紹了Java實現(xiàn)擲骰子控制臺和窗體兩種方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java控制臺和窗體實現(xiàn)擲骰子的具體代碼,供大家參考,具體內(nèi)容如下

程序目標:同時3擲骰子,讓骰子轉(zhuǎn)動若干次后停下來的正面朝上的數(shù)字之和大于9 則為大,小于等于9則為小
用于需要提前選擇押大還是小,程序結(jié)束返回是否押中的結(jié)果。

1.擲骰子控制臺實現(xiàn)

本程序分為三層:

  • 表示層 :用類Player2 實現(xiàn)
  • 業(yè)務(wù)邏輯層:類DiceGame2 實現(xiàn)
  • 數(shù)據(jù)/技術(shù)服務(wù)層:類Dice2 實現(xiàn)

Dice2 類 實現(xiàn)Runnable接口,重新run()方法來設(shè)置每個骰子轉(zhuǎn)動10次 ,并在停下來后獲取正面朝上的值。
DiceGame2  類中創(chuàng)建三個線程來模擬同時擲3個骰子,并能在在骰子轉(zhuǎn)動結(jié)束后得到總點數(shù)和每個骰子的點數(shù)。
其中roll()方法開啟線程,  result()計算點數(shù)
Player2 類 主函數(shù)創(chuàng)建菜單,提示用戶輸入操作。并在控制臺顯示結(jié)果。

代碼如下:

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è)務(wù)邏輯層
 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ù)/技術(shù)服務(wù)層
 private int upnum;
 
 public Dice2() {
  this.upnum = 1;
 }
 
 @Override
 public void run() {
  int count = 10;// 設(shè)置每顆骰子轉(zhuǎn)動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類通過構(gòu)造器初始化設(shè)置窗體和里面控件,并且把代表6種點數(shù)的圖片加載到imgs集合里面,還要給開始按鈕綁定監(jiān)聽事件函數(shù)。
其次,重寫actionPerformed()函數(shù),來實現(xiàn)按鈕觸發(fā)3個骰子轉(zhuǎn)動。在這個函數(shù)里開始3個線程讓骰子圖片轉(zhuǎn)動起來,并在轉(zhuǎn)動結(jié)束后計算機每個骰子顯示圖片對應(yīng)的點數(shù)和總點數(shù)。再根據(jù)用戶之前選擇的押大或押小返回輸贏結(jié)果。
然后,IconThread 類重寫run()方法,來實現(xiàn)圖片的動態(tài)效果。通過隨機生成[0-6)的隨機整數(shù)作為imgs集合的index,再根據(jù)索引從imgs里取對應(yīng)的圖片顯示在窗體上,由于線程運行速度較快,且設(shè)置了多次取圖片的動作從而形成了動態(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("結(jié)果"); //結(jié)果
        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最后圖片對應(yīng)的點數(shù)
              sum += (i + 1);
              break;
          }
      }
      return sum;
  }//      
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis批量update時報錯multi-statement not allow的問題

    mybatis批量update時報錯multi-statement not allow的問題

    這篇文章主要介紹了mybatis批量update時報錯multi-statement not allow的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Java中StringBuilder字符串類型的操作方法及API整理

    Java中StringBuilder字符串類型的操作方法及API整理

    Java中的StringBuffer類繼承于AbstractStringBuilder,用來創(chuàng)建非線程安全的字符串類型對象,下面即是對Java中StringBuilder字符串類型的操作方法及API整理
    2016-05-05
  • JAVA JSP頁面技術(shù)之EL表達式整理歸納總結(jié)

    JAVA JSP頁面技術(shù)之EL表達式整理歸納總結(jié)

    這篇文章主要介紹了java中JSP頁面技術(shù)之EL表達式概念作用以及語法等的使用,需要的朋友可以參考
    2017-04-04
  • Spring?Cloud?+?Nacos?+?Seata整合過程(分布式事務(wù)解決方案)

    Spring?Cloud?+?Nacos?+?Seata整合過程(分布式事務(wù)解決方案)

    Seata 是一款開源的分布式事務(wù)解決方案,致力于在微服務(wù)架構(gòu)下提供高性能和簡單易用的分布式事務(wù)服務(wù),這篇文章主要介紹了Spring?Cloud?+?Nacos?+?Seata整合過程(分布式事務(wù)解決方案),需要的朋友可以參考下
    2022-03-03
  • 詳解Java如何實現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能

    詳解Java如何實現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能

    這篇文章主要為大家詳細介紹了Java如何實現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下
    2023-02-02
  • Springboot?多級緩存設(shè)計與實現(xiàn)方案

    Springboot?多級緩存設(shè)計與實現(xiàn)方案

    多級緩存是提升高并發(fā)系統(tǒng)性能的關(guān)鍵策略之一,它不僅能夠減少系統(tǒng)的響應(yīng)時間,提高用戶體驗,還能有效降低后端系統(tǒng)的負載,防止系統(tǒng)過載,這篇文章主要介紹了Springboot?多級緩存設(shè)計與實現(xiàn),需要的朋友可以參考下
    2024-02-02
  • Mybatis中 mapper-locations和@MapperScan的作用

    Mybatis中 mapper-locations和@MapperScan的作用

    這篇文章主要介紹了Mybatis中 mapper-locations和@MapperScan的作用,mybatis.mapper-locations在SpringBoot配置文件中使用,作用是掃描Mapper接口對應(yīng)的XML文件,需要的朋友可以參考下
    2023-05-05
  • SpringCloud Feign傳遞HttpServletRequest對象流程

    SpringCloud Feign傳遞HttpServletRequest對象流程

    HttpServletRequest接口的對象代表客戶端的請求,當客戶端通過HTTP協(xié)議訪問Tomcat服務(wù)器時,HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學(xué)可以參考下文
    2023-05-05
  • Java實現(xiàn)MD5加密的方式與實例代碼

    Java實現(xiàn)MD5加密的方式與實例代碼

    MD5加密是一種常見的加密方式,我們經(jīng)常用在保存用戶密碼和關(guān)鍵信息上。那么它到底有什么,又什么好處呢,會被這么廣泛的運用在應(yīng)用開發(fā)中
    2021-10-10
  • idea打開項目沒有項目目錄問題及解決

    idea打開項目沒有項目目錄問題及解決

    這篇文章主要介紹了idea打開項目沒有項目目錄問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論