java----猜字符小遊戲

剛學了方法,作了個小項目,猜字符小遊戲,這裏和你們分享一下。
咱們經過要達到的效果來分析:數組

1首先命令行窗口提示用戶進入猜字符遊戲,並顯示「猜吧」
            2而後用戶輸入一個五位數的字符串(不能重複),接着系統進行判斷
            3若是字符串爲"EXIT"則退出遊戲
            4若是沒猜對則提示猜對的字符的個數和猜對位置的個數
            5若是猜對則給出分數,並退出遊戲
            6分數的話是一個字符一百分,猜錯一次扣十分。
咱們來分析:
這個遊戲須要用到哪些數據:
    1.char[] ch 用來接收系統產生的隨機字符
    2.char[] input 用來接收用戶輸入的字符
    3.int score 用來接收分數
    4.int count 用來接收猜錯次數
    5.int[] result 用來存放猜對個數和猜對位置個數
 定義方法:
   1. public static char[] generate(){//用來生成隨機字符數組
                            char[] ch = new char[5];
                            ...
                            return ch;
        }
    2.public static int[] check(char[] ch,char[] input){//用來檢查猜中幾個和猜中位置狀況
                            int[] result = new int[ch.length];
                            ...
                            return result;
    }

    代碼以下:

   1. public static char[] generate(){
                            char[] letter = new char[26];//用來存放A-Z的字符
                            for(int i =0;i<letter.length;i++){
                                        letter[i]=(char)(i+65);//將字母對應編碼強制轉換爲字符並存放在數組中
                            }
                            int index = 0;
                            boolean[] flag = new boolean[letter.length];//用來標記已經選過的字符
                            char[] ch = new char[5];
                            while(index<ch.length){
                                             int i = (int)(Math.random()*letter.length);
                                             if(flag[i]){//排除已經選過的字符
                                                     contiune;
                                             }
                                             ch[index]=letter[i];
                                             index++;
                                             flag[i]=true;
                            }
                            return ch;
        }

        2.public static in[] check(char[] ch,char[] input){
                                int[] result = new int[2];
                                for(int i= 0;i<ch.length;i++){
                                            for(int j=0;j<input.length;j++){
                                                        if(ch[i]==input[j]){
                                                                result[0]++;//猜中字符的個數
                                                                if(i==j){
                                                                        result[1]++;//猜中位置的個數
                                                                        break;//若是位置判斷相同則不用掃描後面的字符了
                                                                }
                                                        }
                                            }
                                }
                                return result;
             }

             接下來開始寫main函數
             public static void main(String[] agrs){
                                 char[] ch = generate();
                                 Scanner sc =new Sacnner(System.in);
                                 int count =0;
                                 int score =0;
                                 System.out.println("歡迎來到猜字符遊戲");
                                 while(true){
                                                System.out.println("猜吧")
                                                String str = sc.nextInt().touppercase();//把輸入的字符串轉換爲大寫
                                                if(str.equal("EXIT")){
                                                            System.out.println("退出");
                                                            break;
                                                }
                                                char input = str.toCharArrays();//字符串轉換爲字符數組
                                                int result = check(ch,input);
                                                if(result[1]==ch.length){
                                                        score = 100*ch.length-10*count;
                                                        System.out.println("猜對了,得分:"+score)
                                                        break;
                                                }
                                                else{
                                                            System.out.println("猜對字符個數爲"+result[0]+"猜對字符位置個數爲"+result[1]);
                                                            count++;
                                                }
                                 }
             }

             這個遊戲還能夠加些小功能,好比經過改變猜想的字符串的長度來改變遊戲難度,或者經過分數來限制遊戲猜想次數等等,這裏就很少寫了,你們能夠嘗試下。
相關文章
相關標籤/搜索