B543a43dc8cb0aae562278484db60d25

Simple program that is supposed to be able to narrow down a "guess" the number a user is thinking of.

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {
	
	public static void main(String[] args)
	{
		int i = 0;
		int LB = 0, UB = 1000;
		boolean eql = false;
		Scanner in = new Scanner(System.in);
		Random r = new Random();
		
		System.out.println("Think of a number between 0 and 1000");
		
		int res = r.nextInt(UB); //first guess between 0 - 1000
	
		System.out.println("Is it <, >, or = to " + res);
		i++;
		String input = in.nextLine(); //find if the the guess is higher, lower or equal to users number
		
		while (!eql == true) //need to keep guessing whilst the value guessed is wrong 
		{
		
			if (input.equals ("<")) //if guess needs to be lower
			{
				UB = res;
				res = LB + (r.nextInt(UB - LB));
				System.out.println("Is it <, >, or = to " + res);
				i++;
				input = in.nextLine();
			}
			if (input.equals (">")) // if guess needs to be higher
			{
				LB = res;
				res = LB + (r.nextInt(UB - LB));
				System.out.println("Is it <, >, or = to " + res);
				i++;
				input = in.nextLine();
			}
			if (input.equals ("=")) //if guess is equal to, game is finished
			{
				System.out.println("It took me " + i + " guesses");
				eql = true;
			}
		}
	}

}

Refactorings

No refactoring yet !

B066cb3c505933f832faa83238489a89

halogenandtoast

October 27, 2008, October 27, 2008 15:34, permalink

No rating. Login to rate!

Reduced your code a bit, nothing special

import java.util.Scanner;
import java.util.Random;

public class GuessingGame {
	
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		Random r = new Random();
		
		int i = 1, LB = 0, UB = 1000;
		int res = r.nextInt(UB); //first guess between 0 - 1000
		
		System.out.println("Think of a number between 0 and 1000");
		System.out.println("Is it <, >, or = to " + res);
		String input; //find if the the guess is higher, lower or equal to users number
		
		while (!(input = in.nextLine()).equals("=")) //need to keep guessing whilst the value guessed is wrong 
		{
			if (input.equals ("<")) //if guess needs to be lower
			{
				UB = res;
			}
			else if (input.equals (">")) // if guess needs to be higher
			{
				LB = res;
			}
			res = LB + (r.nextInt(UB - LB));
			System.out.println("Is it <, >, or = to " + res);
			i++;
		}
		System.out.println("It took me " + i + " guesses");
	}

}
Ecb440cab118ff2af192eeda62173382

RG

October 27, 2008, October 27, 2008 18:20, permalink

1 rating. Login to rate!
import java.util.Scanner;
import java.util.Random;

public class GuessingGame {
    int guessCount = 0;
    Scanner cin = new Scanner(System.in);
    Random rand = new Random();
    
    public static void main(String[] args) {
	GuessingGame game = new GuessingGame();
	int guesses = game.go(1000, 0);
	System.out.println("It took me " + guesses + " guesses" );
    }
    public int go(int upperBound, int lowerBound) {
	guessCount++;
	int newGuess = getBoundedRand( upperBound, lowerBound );
	System.out.println( "Is this number greater than ('>') less than ('<') or equal to ('=') your number?\n" + newGuess );
	String input = cin.nextLine();
	if (input.equals("<")) {
	    lowerBound = newGuess;
	} else if (input.equals(">")) {
	    upperBound = newGuess;
	} else {
	    return guessCount;
	}
	return go( upperBound, lowerBound );
    }
    private int getBoundedRand(int upperBound, int lowerBound) {
	return lowerBound + (rand.nextInt(upperBound - lowerBound));
    }
}
D41d8cd98f00b204e9800998ecf8427e

rock

April 30, 2009, April 30, 2009 02:56, permalink

No rating. Login to rate!
import java.io.*;
public class guess_num
{

  public static void main(String args[])
  {
   int cnum[]=new int[4],unum[]=new int[4];
   int times,a,b,user_num=0;                     
   
   for(int i=0;i<50;i++)
             System.out.print("*");
             System.out.println("\n");
   System.out.println("猜数字游戏,计算机将生成一个无重复的四位整数(1~9)。\n");
   System.out.println("          2008-2-13    兰哲");
   for(int i=0;i<50;i++)
             System.out.print("*");
             System.out.println();
            
      do{                                                   //生成随机数
          cnum[0]=(int)(Math.random()*10+1);
          cnum[1]=(int)(Math.random()*10+1);
          cnum[2]=(int)(Math.random()*10+1);
          cnum[3]=(int)(Math.random()*10+1);
          for(int i=0;i<4;i++)
          if(cnum[i]==10)
          cnum[i]--;       //不包括10
              if(cnum[0]==cnum[1]||cnum[0]==cnum[2]||cnum[0]==cnum[3]||cnum[1]==cnum[2]||cnum[1]==cnum[3]||cnum[2]==cnum[3])       //保证获取的随机数无重复
                 continue;
              else
                 break;
          }while(true);                                        //生成随机数结束
   
    for(times=1;;times++)                              //用户动作
      {
      String s="";
   try{      
    System.out.print("请输入一个不包括零的无重复的四位整数,按回车确定:");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    s=br.readLine();
    user_num=Integer.parseInt(s);
   }catch(IOException e){}
   a=0;
   b=0;
  
  
   if(user_num==7758258)                //输入7758258显示答案
   {
    System.out.print("答案为:");
    System.out.print(cnum[0]);
    System.out.print(cnum[1]);
    System.out.print(cnum[2]);
    System.out.print(cnum[3]);
    System.out.println();
    times--;
    continue;
   }
   else if(user_num>=1000&&user_num<=9999)             
     {
         unum[0]=user_num/1000;
         unum[1]=user_num/100-unum[0]*10;
         unum[2]=user_num/10-unum[0]*100-unum[1]*10;
         unum[3]=user_num%10;    
                                     
                   if(unum[0]==unum[1]||unum[0]==unum[2]||unum[0]==unum[3]||unum[1]==unum[2]||unum[1]==unum[3]||unum[2]==unum[3])     //保证输入的数无重复
                       { 
                         System.out.println("出现重复数字,请重新输入。");
                            times--;
                           continue;
                       }                                   
         for(int aa=0;aa<4;aa++)          //aa,bb作为数组底数,用于比较正确个数   一次for语句仅用于一次数值判断获取a,b的值
         {
                                    //a为数值正确且位置正确,b为数字正确,但位置不正确
            for(int bb=0;bb<4;bb++)                    //统计猜正确的数的个数。
            {
              if(unum[aa]==cnum[bb])
              {
                    if(aa==bb)
                    a++;
                    else
                    b++;
                  break;
              }
              else
               continue;
            }
         }
       }
   else
        {
          System.out.print("输入错误,重新输入。");
          times--;
          continue;
        }
           
          System.out.println();
          System.out.print("猜对");
          System.out.print(a);
          System.out.print("个数字且位置也对");
          System.out.print(" 位置不对但数字对的有");
          System.out.print(b);
          System.out.println("个。\n");
            if(a==4)
            {
             for(int i=0;i<50;i++)
             System.out.print("*");
             System.out.println("\n");
             System.out.print("恭喜你!你猜对了!");
             System.out.print("用了");
             System.out.print(times);
             System.out.println("次。\n");
             for(int i=0;i<50;i++)
             System.out.print("*");
             System.out.println("\n");
             break;
             }
       }
 

      }
   
 
}
336dc9b9daa024d7c1733ba039ea200d

Eunseok

March 5, 2010, March 05, 2010 03:45, permalink

No rating. Login to rate!

Your Program Source is happen Excetion : IllegalArgumentException

try{
	res = LB + (r.nextInt(UB - LB));
}catch(IllegalArgumentException num){
	System.out.println("It's Exception");
};

Your refactoring





Format Copy from initial code

or Cancel