2022/07/14
4263

Java程式教室(三):用JAVA寫出擲骰子遊戲

Java程式密技-擲骰子

擲骰子遊戲

當我們學習任何程式的語法之後,都議定會碰到一個瓶頸…..我要如何加強提升我的開發能力呢?
 
這裡我分享我的經驗,從生活中找範例,想辦法將平常生活有趣的或常態的習慣或經驗或興趣,看能否透過程式撰寫讓機器知道你的想法與目的,如此不但能加強你的邏輯訓練,而且在想解決方法時,你會發現之前不懂的項目,突然間就懂了。

擲骰子,大家應該都有這個經驗,以下是一個簡單的遊戲,規則如下:
 
玩家質兩個骰子,點數為1~6,如果第一次點數和為7或11,則玩家勝,如果點數和為2,3或12,則玩家輸,如果和為其他點數,則記錄第一次的點數和,然後繼續擲骰,直到點數和等於第一次擲出的點數和,則玩家勝,如果在這之前擲出了點數和為7,則玩家輸。
 

問題分析:

可使用switch搭配if判斷即可,小心條件的順序
 

操作步驟

執行需求步驟
Step1
提示玩家按 “enter”開始遊戲

D00010-1

Step 2-擲骰子,玩家輸,按任意鍵繼續玩

D0010-2

Step 3-擲篩子,一直到玩家勝為止,按n結束遊戲

D0010-3
 

參考解答:

---
 
import java.io.IOException;
 
class CrapEx
{
        public static void main(String[] args) throws IOException{
 
                final int WAN=0,Lost=1,Mid=2;
                boolean flag=true;
                int Outcome=Mid;
                int sum;
                int point=0;
                System.out.println("擲骰子遊戲,按enter鍵開始遊戲****");
                while(true)
                {
                        System.in.read();
                        if(flag){
                                sum=getPoint();
                                System.out.println("第一次玩家擲出的點數和:"+sum);
                                switch(sum)
                                {
                                        case 7:
                                        case 11:
                                                Outcome=WAN;
                                                break;
                                        case 2:
                                        case 3:
                                        case 12:
                                                Outcome=Lost;
                                                break;
                                        default:
                                                flag=false;
                                                Outcome=Mid;
                                                point=sum;
                                                break;     
                                }
 
                        }
                        else
                        {
                                sum=getPoint();
 
                                System.out.println("\n第二次玩家擲出點數和:"+sum);
                                if(sum==point)
                                {
                                        Outcome=WAN;
                                }
                                else
                                {
                                        Outcome=Lost;
                                }
                        }
 
                        if(Outcome==Mid)
                        {
 
                                System.out.println("未分勝負,在擲一次****");
                        }
                        else
                        {
                                if(Outcome==WAN)
                                {
                                        System.out.println("玩家勝");
                                }
                                else
                                {
                                        System.out.println("玩家輸");
                                }
                                System.out.print("在玩一次?");
 
                                if(System.in.read()=='n')
                                {
                                        System.out.println("遊戲結束");
                                        break;
                                }
                                flag=true;
                        }
                }
        }
        public static int getPoint()
        {
                int roll=((int)(Math.random()*6)+(int)(Math.random()*6));
                if(roll<2)
                {
                        roll=2;
                }
 
                return roll;
        }
 
}

---
 

>>程式課程試聽索取 

>>職訓補助課程


 



  江聖榮 老師
  專長:

  程式設計:從前端(UI front-end)到後端(Back-end)之全端工程師(full-stack)
  精通語言:Java、Javascript 前後端框架   
  開課詳情

 
LINE-friend
近期文章熱搜文章排行
熱門標籤熱門標籤
近期文章你可能有興趣的文章
熱門標籤熱門標籤