發新話題

[JAVA] 猜數字遊戲

[JAVA] 猜數字遊戲

猜數字遊戲的程式碼....@@
寫得爛還多指教/__\
引用:
import java.io.*;
public class NumGame
{
        public static void main(String[] args) throws IOException
        {
               
                BufferedReader keyin =
                        new BufferedReader(new InputStreamReader(System.in));
               
                String Str1 = new String();
                for (int f=0;f<=2 ;f++ )
                {
                        //產生亂數
                        double a = Math.random()*10;
                        int a1 = (int)a;
                        double b = Math.random()*10;
                        int a2 = (int)b;
                        double c = Math.random()*10;
                        int a3 = (int)c;
                        double d = Math.random()*10;
                        int a4 = (int)d;
                        if (a1==a2||a1==a3||a1==a4||a2==a3||a2==a4||a3==a4)
                        {
                                f=0;
                        }
                        else
                        {
                                System.out.println();
                                System.out.println();
                                System.out.println("\t猜數字遊戲,請輸入四個數字");
                                System.out.println();
                                System.out.println("\t*********遊戲規則*********");
                                System.out.println();
                                
System.out.println("如數字相同且位置相同則為A,數字相同位置不同則為B,均不同則不顯示");
                                for (int z=0;z<=4 ;z++)
                                {
                                        while(true)
                                        {
                                                System.out.println();
                                                System.out.print("請輸入四個數:");
                                                Str1 = keyin.readLine();
                                                System.out.println();
                                                int len = Str1.length();//取得字串長度
                                                if (len!=4)
                                                {
                                                        System.out.println("你輸入的數字,不是四位數,請重新輸入!!");
                                                }
                                                else
                                                {
                                                        char num1 = Str1.charAt(0);
                                                        char num2 = Str1.charAt(1);
                                                        char num3 = Str1.charAt(2);
                                                        char num4 = Str1.charAt(3);
                                                        if (num1 > '9'|| num2 > '9'|| num3 > '9'|| num4 > '9')
                                                        {
                                                                System.out.println("你輸入的不是數字,請重新輸入!!");
                                                        }
                                                        else
                                                        {
                                                                break;//離開while的迴圈
                                                        }
                                                }
                                        }
                                        int e = Integer.parseInt(Str1);
                                        int e1 =e/1000;
                                        int e2 =e%1000/100;
                                        int e3 =e%100/10;
                                        int e4 =e%10;
                                        int y=z=0;
                                        if (e1==a1)
                                        {
                                                z=z+1;
                                        }
                                        else if (e1==a2 || e1==a3 ||  e1==a4)
                                        {
                                                y=y+1;
                                        }
                                        if (e2==a2)
                                        {
                                                z=z+1;
                                        }
                                        else if (e2==a1 || e2==a3 || e2==a4)
                                        {
                                                y=y+1;
                                        }
                                        if (e3==a3)
                                        {
                                                z=z+1;
                                        }
                                        else if (e3==a1 || e3==a2 ||e3==a4)
                                        {
                                                y=y+1;
                                        }
                                        if (e4==a4)
                                        {
                                                z=z+1;
                                        }
                                        else if (e4==a1 ||  e4==a2 || e4==a3)
                                        {
                                                y=y+1;
                                        }
        
                                        System.out.println("你輸入的是: "+e1+e2+e3+e4 + "電腦提示: "+z+"A"+y+"B");
                                        System.out.println();
                                }

                                

                                for ( f=0; f <= 2 ; f=f+1 )
                                {
                                        System.out.print("妳答對了,是否繼續(y,n)");
                                        char r =(char)System.in.read();
                                        System.in.read();System.in.read();
                                        if (r=='y' || r=='Y')
                                        {
                                                System.out.println("你輸入繼續");
                                                f=1;
                                                break;
                                        }
                                        else if (r=='n' || r=='N')
                                        {
                                                System.out.println("結束運算");
                                                f=100;
                                        }
                                        else if (f==2)
                                        {
                                                System.out.println("你是豬哦!Y OR N 都搞不清楚,為了加強你的腦部訓練,所以再玩一次!");
                                                f=1;
                                                break;
                                        }
                                        else
                                        {
                                                System.out.println("你輸入的非y or n;請重新輸入");
                                        }
                                }
                        }
                }

        }
}

TOP

感謝~有您的付出~我們就有動力~~謝謝~~

TOP

v.v  正在學習中  感謝分享

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。