- package guessNumber;
- import java.util.Random;
- import java.util.Scanner;
- public class Guess {
- public static int black;
- public static int white;
- public static void main(String[] args) {
- System.out.println("游戏规则:系统随机给出若干1-n的数字,玩家猜。\\n" +
- "若对应位置上的数字正确则显示●,若数字正确但是位置不正确则显示○,若都不正确则不显示。" +
- "\\n注:●的位置并不代表数字的位置。");
- int boxSize;
- int numRange;
- Scanner s = new Scanner(System.in);
- System.out.println("输入盒子的大小:");
- boxSize = s.nextInt();
- System.out.println("输入产生随机数的范围:");
- numRange = s.nextInt();
- int[] box = new int[boxSize];
- //Random r = new Random();
- //产生可能相同的随机数
- box = createRandom(boxSize,numRange);
- for(int i=0;i<box.length;i++){
- System.out.print(box[i]+" ");
- }
- int[] guess = new int[boxSize];
- System.out.println("===========================================");
- System.out.println("游戏开始,请输入"+boxSize+"个1~"+numRange+"的数字,由空格分开。");
- do{
- black=0;
- white=0;
- int i;
- //input random number
- for(i =0;i<guess.length;i++){
- guess[i] = s.nextInt();
- }
- //output the number in box
- /*for(i=0;i<box.length;i++){
- System.out.print(box[i]+" ");
- }*/
- int[] sbox = new int[boxSize];
- int[] tempOfBox = new int[boxSize];
- int[] sguess = new int[boxSize];
- int[] tempOfGuess = new int[boxSize];;
- copy(box,tempOfBox);
- sbox = sort(tempOfBox);
- copy(guess,tempOfGuess);
- sguess = sort(tempOfGuess);
- compareBlack(box,guess);
- compareWhite(sbox,sguess);
- white = white - black;
- //System.out.println("black:"+black);
- //System.out.println("white:"+white);
- for(int a=0;a<black;a++){
- System.out.print("●");
- }
- for(int b=0;b<white;b++){
- System.out.print("○");
- }
- if(!(white==0&&black==0)){
- System.out.println();
- }
- }while(black!=boxSize);
- System.out.println("恭喜你,盒子里的数字就是:");
- for(int k=0;k<box.length;k++){
- System.out.print(box[k]+" ");
- }
- }
- public static int[] sort(int[] a){
- for(int i=0;i<a.length-1;i++){
- for(int j=i+1;j<a.length;j++){
- if(a[i]>a[j]){
- int temp;
- temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- return a;
- }
- public static void compareBlack(int[] a,int[] b){
- for(int i=0;i<a.length;i++){
- if(a[i]==b[i]){
- black++;
- }
- }
- }
- public static void compareWhite(int[] sa,int[] sb){
- int a,b;
- int f=0;
- for(a=0;a<sa.length;a++){
- for(b=f;b<sb.length;b++){
- if(sa[a]==sb[b]){
- f=b+1;
- white++;
- break;
- }
- }
- }
- }
- public static void copy(int[] a,int[] copy){
- for(int i=0;i<a.length;i++){
- copy[i] = a[i];
- }
- }
- public static int[] createRandom(int boxSize,int numRange){
- int[] box = new int[boxSize];
- Random r = new Random();
- for(int a=0;a<boxSize;a++){
- box[a] = r.nextInt(numRange)+1;
- }
- return box;
- }
- public static int[] createRandomDifferent(int boxSize,int numRange){
- int[] box = new int[boxSize];
- Random r = new Random();
- boolean flag = false;
- for(int i=0;i<box.length;i++){
- box[i] = r.nextInt(numRange)+1;
- for(int j=0;j<=i;j++){
- if(box[i]==box[j]){
- flag = true;
- break;
- }
- }
- if(flag){
- i--;
- continue;
- }
- }
- return box;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/261220138278.html
来源: http://www.codesnippet.cn/detail/261220138278.html