close

先預告一下2.0版的修正和想要新增的功能:

修正

  1. 一次可以輸入3位數
  2. 不接受數字以外字元
  3. 玩家輸入的數字不能重複 舉例:122
  4. 加入0成為可以猜的數字之一

新功能

  1. 猜過的數字歷史紀錄
  2. 猜4位數字
  3. 新手教學
  4. 次數限制 - X步內一定要猜出答案否則GG
  5. 猜中或GG之後,詢問是否再玩一遍

 

呼~會不會一次提太多了,主要是修正的部份必須做好,不然程式還蠻容易當掉的。另外現在export出來的.jar檔,似乎不能在沒有裝JRE的電腦裡執行,不知道有沒有辦法存成.exe?

 

9/25 Update

今天跟小P老師請教之後,得到了幾個關鍵的方向:

1. 要讓多位數一次跟多位數比較,必須用到陣列

2. 比對完"A"之後,可以將得到"A"的位數排除,其他位數做比較就好

3. 玩家輸入的數字不能重複(防呆),要用陣列的第二位數跟第一位比取布林值,再用第三位比第一位

4. 次數限制:每跑一次迴圈都將計數器加1,到第x次之後就可被計算了

 

 

9/27 Update

經過老師講解,主要是用數學運算的方式萃取4位數成為個別數字後,加以比較

package com.sun.www;

import javax.swing.JOptionPane;

public class GuessNumber2 {

 

    public static void main(String[] args) {

        int a = (int) (Math.random() * 10);

        int b = (int) (Math.random() * 10);

        while (b == a) {

            b = (int) (Math.random() * 10);

        }

        int c = (int) (Math.random() * 10);  //Math.random的值是0~0.999999, 乘以10, 可以得到0~9.99,強制轉型int之後去掉小數點,符合我們要猜的數字的範圍

        while (c == a || c == b) {

            c = (int) (Math.random() * 10);

        }

        int d = (int) (Math.random() * 10);

        while (d == a || d == b || d == c) {

            d = (int) (Math.random() * 10);

        }

 

        System.out.println("偷看答案=" + a + b + c + d);    //偷看答案

            int times = 0;

            

        do {

            

            String input = JOptionPane.showInputDialog("請輸入四位數字:");

            int userInput = Integer.parseInt(input);

            

            if(userInput>10000||userInput<999){      //位數錯誤防呆  輸入0開頭會有問題;輸入0000也有問題 //應該用偵測字串長度String.length

                JOptionPane.showMessageDialog(null,"請輸入四位數字");

                continue;

            }

            

                       

            

            int e = userInput / 1000;

            int f = (userInput % 1000) / 100;

            int g = (userInput - e * 1000 - f * 100) / 10;

            int h = userInput % 10;

            

            if(f==e||g==e||h==e||g==e||g==f||g==h){                   //數字重覆防呆

                JOptionPane.showMessageDialog(null, "輸入數字重複,請重新輸入");    

                continue;

            }

            

          

            System.out.println("偷看imput分解=" + e + f + g + h);      //偷看userInput分解

 

            int scoreA = 0, scoreB = 0;

 

            if (a == e) {

                scoreA++;

            }

            if (b == f) {

                scoreA++;

            }

            if (c == g) {

                scoreA++;

            }

            if (d == h) {

                scoreA++;

            }

 

            if (a == f || a == g || a == h) {

                scoreB++;

            }

            if (b == e || b == g || b == h) {

                scoreB++;

            }

            if (c == e || c == f || c == h) {

                scoreB++;

            }

            if (d == e || d == f || d == g) {

                scoreB++;

            }

 

            JOptionPane.showMessageDialog(null, "你的成績是 = " + scoreA + "A" + scoreB + "B");

            times++;

            if(times==3){

               int gg = JOptionPane.showConfirmDialog(null, "GG了!要再玩一遍嗎?");

               if(gg==0){

                   continue;

               }else{

                break;

               }

                

            }

            if (scoreA == 4) {

                int bingo = JOptionPane.showConfirmDialog(null, "猜中了!要再玩一遍嗎?");

                if(bingo==0){

                   continue;

               }else{

                break;

               }

               

            }

 

        } while (true);    //當以上都成立的時候,繼續跑do迴圈(不指定次數)

 

    }

 

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鈴木保齡球 的頭像
    鈴木保齡球

    Java程式學習手札

    鈴木保齡球 發表在 痞客邦 留言(0) 人氣()