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 !
halogenandtoast
October 27, 2008, October 27, 2008 15:34, permalink
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");
}
}
RG
October 27, 2008, October 27, 2008 18:20, permalink
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));
}
}
rock
April 30, 2009, April 30, 2009 02:56, permalink
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;
}
}
}
}
Simple program that is supposed to be able to narrow down a "guess" the number a user is thinking of.