论坛首页· 友情链接申请·申请版主· 广告投放· 道具中心· 设为首页· 收藏本站
发新话题
打印

猜数字游戏

猜数字游戏

复制内容到剪贴板
代码:
//named  GuessNumber.java
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
        public static void main(String[] args) {
           int[] a = fun.createNumber();
          System.out.println("Start :");
          fun.compare(a);
        }
     static class fun {
        public static int A = 0;
        public static int B = 0;
        public static int n = 0;
                /** 产生一个随机数(1000-9999)** */
        private static int getRandNumber() {
            int n = -1;
           Random rd = new Random();
           n = rd.nextInt(9000) + 1000;
           return n;
        }
                            /* 获得一个有效数组 */
static int[] createNumber() {
                int n = fun.getRandNumber();
                //System.out.println(n);
        int[] a = {  n % 10, (n / 10) % 10,(n / 100) % 10,  n / 1000};
                for (int j = 0; j < a.length; j++) {
                  for (int k = j + 1; k < a.length; k++)
                        if (a[j] == a[k])
                        a=createNumber();
                        }
                return a;
                }
                /* start */
        static void compare(int a[]) {                        
                        for (n = 0; A != 4 && n < 8;) {
                                A = 0;
                                B = 0;
                        Scanner scanner = new Scanner(System.in);
                                int temp;
        for (temp = 0; temp > 9999 || temp < 1000;
                                       temp = scanner.nextInt())
        System.out.println("\ninput a number your guess(between 1000-9999):");

        int temp1[] = { temp % 10, (temp / 10) % 10, (temp / 100) % 10,
                                temp / 1000 };
                for (int i = 0; i < temp1.length; i++) {
                for (int j = 0; j < temp1.length; j++) {
                        if (a[i] == temp1[j] && i == j) {
                                        A++;
                                      continue;
                                        }
                                else if (a[i] == temp1[j]&& i!=j)
                                        B++;
                                        }

                                }

                                n++;
                                if (A == 4)
                                System.out.println("恭喜您猜数成功");
                                else if (n < 8)
                                System.out.print(A+"A"+B+"B");
                                else
                                {System.out.println(A+"A"+B+"B");
                        System.out.println("Time Up,Game Over\n  ");
                                   for(int i=0;i
                                        System.out.print(a);
                                   }
                                }
                        }

                }

        }
}
[ 本帖最后由 阿西木 于 2008-6-9 10:43 编辑 ]

TOP

好玩吗,下来玩玩!!!

TOP

回复 2# 的帖子

呵呵,就一个跟平时玩的猜数字游戏差不多

TOP

发新话题