日期:2014-05-20  浏览次数:20712 次

新兵蛋子--JAVA猜数游戏
猜数游戏,系统随机生成一个的三位数( 其中: 百位不为0 ),然后由你猜测的三位数。
如果你猜中了某一位,那么就将该位的数字显示出来,未猜对的位上,则显示 '#'

------解决方案--------------------
给你个差不多的代码(你添加一个方法:猜中了就显示出来,如果没猜中就再未猜中的位数显示#就可以了)
Java code
package csdn.kao331431214.number4;

import java.util.Scanner;

public class HiLo {

    /**
     * @author 小明
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int x, y, z = 1;//x为随机数 y为输入数 z为是否猜对的监视器
        String str = "y";//控制程序总循环

        Scanner scan = new Scanner(System.in);//输入数字用
        Scanner scan1 = new Scanner(System.in);//输入字符串用

        while(str.equalsIgnoreCase("y")){//程序总循环
            
            x = (int)(Math.random() * 100 + 1);//产生随机数字
            System.out.println("猜猜看,数字是多少?");
            y = scan.nextInt();

            if(y > 0 && y < 101){//数字输入正确
                while(z != 0){

                    if(y == x){
                        System.out.println("恭喜你猜对了!!");
                        z = 0;
                    }else{//判断大小
                        if(y > x){
                            System.out.println("再小点~");
                        }else{
                            System.out.println("再大点~");
                        }
                    System.out.println("猜猜看,数字是多少?");
                    y = scan.nextInt();
                    
                    }                        
                }
            }else{//数字输入错误
                System.out.println("输入数字错误,必须大于0小于等于100");
                System.out.println("程序重新启动中");
                System.out.println();
            }


            if(y == x){//控制总循环
                System.out.println("继续吗?(Y/N)");
                str = scan1.nextLine();
            }
        }
    }

}

------解决方案--------------------
Java code

package test;

import java.util.Scanner;

public class TestReg {
    public static void main(String[] args) {
        int a = (int) (Math.random() * 1000);
        String bString, aString = "";
        int j = 0;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("请输入三位数字");
            bString = input.next();
            j++;
            if (j == 3)
                System.exit(0);
        } while (!bString.matches("[1-9]{1}[0-9]{2}"));
        System.out.println("随机数=" + a);
        bString = a + bString;
        for (int i = 0; i < 3; i++) {
            if (bString.charAt(i) == bString.charAt(i + 3))
                aString = aString + bString.charAt(i);
            else
                aString = aString + "#";
        }
        System.out.println(aString);
    }
}

------解决方案--------------------
Java code

package testWeb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class GuessNum {
    // 生成随机三位数
    private static int randomNum() {
        int result = 0;
        // 以下任一方法均可产生100<=&&<=999的正整数
        // Random r = new Random();
        // do {
        // result = Math.abs(r.nextInt(1000));
        // } while (result < 100);
        do {
            result = (int) (1000 * Math.random());
        } while (result < 100);
        return result;
    }

    //
    public static boolean haveStar(String[] temp) {
        boolean result = false;
        for (int i = 0; i < temp.length; i++) {
            if (temp[i].equals("*"))
                result = true;
        }
        return result;

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = randomNum();
        e);