Welcome, guest | Sign In | My Account | Store | Cart

This program is a game in which someone will try and guess a number between 1-100 in 4 tries. It will also give out instructions of how to play using methods and will also tell the person if their guess was too low or too high.

Java, 95 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
public class Program12
 {
 	static int guess;
 	static int randnum;
 	static int counter;
 	static boolean finished; 		 	
 	
 	public static void main(String[] args)
 	{
 		int closest=100;
 		int tempclosest=100;
 		Instructions();
 		randnum=(int)(Math.random()*100)+1;
 		counter=1;
		EasyReader console=new EasyReader();
		
		finished=false;
		while(!finished && counter!=5)
		{
		System.out.println();
		System.out.print("What's your guess? ");
		guess=console.readInt();
		
		 if(guess<randnum)
		  {tooLow();
		   tempclosest=randnum-guess;
		   if(tempclosest<closest)
		   closest=tempclosest;
		  }
		 if(guess>randnum)
		  {tooHigh();
		   tempclosest=guess-randnum;
		   if(tempclosest<closest)
		   closest=tempclosest;
		  }
		  
		 if(guess==randnum)
		  {youWin();
		  
		  }
		 else
		 {
		 if(counter==4)
		  youLose(closest);
		 }
		 counter++;
		}
	}
	
 		
 	public static void Instructions()
 	{
 		Util.center("THE SUPER DOOPER SECRET NUMBER GAME!!!");
 		System.out.println();
 		System.out.println("Hi, and welcome to the secret number game");
 		System.out.println("The rules of this game are simple");
 		System.out.println("1. Guess a number between 1-100");
 		System.out.println("2. Then choose another number based on if you were too HIGH or too LOW");
 		System.out.println("3. Also you only have 4 guesses so BE SMART!!!");
 		System.out.println();
 		System.out.println("Ok I am thinking of a number between 1-100");
 	}

	public static void tooHigh()
	{
		System.out.println("TO HIGH TRY AGAIN(choose a lower #)");
	}
	
	public static void tooLow()
	{
			System.out.println("TO LOW TRY AGAIN(choose a higher #)");
	}
	
	public static void youWin()
	{
		System.out.println("YOU GUESSED IT in " +counter+" tries!!!");
		finished=true;
		
	}
	
	public static void youLose(int x)
	{	
		 finished=true;
		 System.out.println();
		 System.out.println("Sorry you did not guess correctly in 4 tries");
		 if(x==1)
		 {System.out.print("Now doesn't that stink, you were only one away from");
		 System.out.println(" the secret number!");}		 
		 else
		 {System.out.println("Your closest guess was "+Math.abs(x)+" away");
		 System.out.println("...PLEASE TRY AGAIN...");
		 System.out.println("The secret number was "+randnum);}
		 System.out.println();
	}
}

1 comment

mhuffnagle 19 years, 2 months ago  # | flag

Not Python. Why is a Java program listed in the Python Cookbook?

Created by Ohm Patel on Sun, 20 Feb 2005 (PSF)
Java recipes (20)
Ohm Patel's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks