[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.1

Random Number

Asked by LyonJay in New to Java Programming

Dear all,

Below is my code:


The issue i am having is with the Random Number generator.

All it seems to generate is 0 and that's the only way to win.

Could someone help me out in getting the code to generator other numbers that 0 but between 0 - 100.

Regards

Lyon JT
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:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
//
//  GuessAndWin.java
//  GuessAndWin
//
//  Created by Lyon Jay Till on 18/02/2009.
//  Copyright (c) 2009 __MyCompanyName__. All rights reserved.
//
//  A simple signed Java applet
//
 
import java.awt.*;
import java.awt.BorderLayout.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.Component.*;
import java.awt.event.*;
import java.util.Random.*;
import java.lang.Math;
 
public class GuessAndWin extends JApplet implements ActionListener
	{
		
		/* Visible elements */
		
		/* Text boxs */
		JTextField theGuess = new JTextField(3); 
		JTextField thePlayer = new JTextField(20);
		
		/* Labels */
		JLabel bankRoll = new JLabel("100.00 Pounds");
		
		/* Buttons */
		JButton newPlayer = new JButton("New Player");
		JButton newNumber = new JButton("New Number");
		
		/* Textarea */
		JTextArea theOutput = new JTextArea();
		//theOutput.setVisible(true);
		
		/* None Visible Elements */
		
		/* Player name */
		String playerName = new String();
		
		/* Computers number */
		int theNumber;
		
		/* Number of tries & games */
		
		int numTries = 0;
		int numGames = 0;
		
		/* Amount of current players bankroll */
		
		double amtRemaining = 100.0;	
		
		/* Random number for the computer */
		
		Random randomizer = new Random();
		
		
		public void init()
		{
			setLayout(new BorderLayout(30,30)); // Creates a method & object of BorderLayout(Built in class)
			
			/* Layout */
			
			/* Making object of panel */
			Panel p1 = new Panel();
			Panel p2 = new Panel();
			
			/* Labels */
			JLabel mgLbl = new JLabel("Make your guess: ");
			
			
			/* Adding */
			
			/* p1 */
			
			/* Labels */
			p1.add(mgLbl);
			
			/* Other */
			p1.add(theGuess);
			p1.add(bankRoll);
			
			/* p2 */
			/* Other */
			p2.add(newPlayer);
			p2.add(thePlayer);
			p2.add(newNumber);
			
			/* p1 */
			/* Add the top bar */
			add("North", p1);
			/* p2 */
			/* Add the bottom bar */
			add("South", p2);
			
			/* Dummy JLabels & Adding */
			/* Left side */
			add("West", new JLabel("		"));
			/* Right side */
			add("East", new JLabel("		"));
			
			/* Textarea & Adding */
			add("Center", theOutput);
			theOutput.revalidate();
			theOutput.repaint();
			
			/* End of Layout */
			
			/* Methods */
			
			newPlayer();
		}
		
		public GuessAndWin() // Action listener
		{
			/* Adding actionListener */
			
			/* Buttons */
			newPlayer.addActionListener(this);
			newNumber.addActionListener(this);
			/* Textbox */
			thePlayer.addActionListener(this);
			theGuess.addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e)
		{
			
			if(e.getSource() == newPlayer)
			{
				newPlayer();
			}
			else if(e.getSource() == thePlayer)
			{
				addPlayer();
			}
			else if(e.getSource() == newNumber)
			{
				newGame();
				theGuess.setEnabled(true);
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				thePlayer.setEnabled(false);
				
			}
			else if(e.getSource() == theGuess)
			{
				newGuess();
			}
			
		}
		
		
		public void newPlayer()
		{
			/* Clear the the output textfield */
			theOutput.setText("");
			theOutput.append("Enter your name in the text box below and press Return");
			
			/* Disable the ability to type text in this area */
			theOutput.setEnabled(false); //The text area in the centre has now been disabled
			theGuess.setEnabled(false);
			newPlayer.setEnabled(false);
			newNumber.setEnabled(false);
			
			/* Set the colour of the text box */
			theGuess.setBackground(Color.white);
			
			/* Enable the player textfield */
			thePlayer.setEnabled(true);
			thePlayer.setText("");
			
			/* Set the keyboard focus & background color on thePlayer */
			thePlayer.requestFocus(true);
			thePlayer.setBackground(Color.white);
			
		}
		
		public void addPlayer()
		{
			playerName = thePlayer.getText();
			equals();
		}
		
		public void newGame()
		{
			numGames = numGames + 1;
			
			int randomInt = randomizer.nextInt(100);
			randomInt = theNumber;
			
			displayInstructions();
			
			amtRemaining = amtRemaining - 1.00;
			
			updateScore();
		}
		
		public void newGuess()
		{
			theOutput.setText("");
			
			String curStr = theGuess.getText();
			int curGuess = Integer.parseInt(curStr);
			
			numTries = numTries + 1;
			
			if(curGuess == theNumber)
			{
				gameWon();
			}
			else
			{
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				
				switch (numTries)
				{ 
					case 1:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n8 attempts remaining!");
						break; 
						
					case 2:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n7 attempts remaining!");
						break;
						
					case 3: 
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n6 attempts remaining!");
						break;
						
					case 4:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n5 attempts remaining!");
						break;
						
					case 5: 
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n4 attempts remaining!");
						break;
						
					case 6:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n3 attempts remaining!");
						break;
						
					case 7:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n2 attempts remaining!");
						break;
						
					case 8:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n1 attempt remaining!");
						break;
						
					default:
						theOutput.setText("");
						theOutput.append("********** Loser **********");
						theOutput.append("\nSorry "+ playerName + " you are not as good as you think you are!");
						theGuess.setEnabled(false);
						break;
						
				}
				
			}
		}
		
		
		public void equals()
		{
			if(playerName == "")
			{
				theOutput.append("You must insert a name before you can play");
			}
			else
			{
				amtRemaining = 100.0;
				numGames = 0;
				
				/* Calling methods */
				newGame();
				updateScore();
				
				thePlayer.setEnabled(false);
				thePlayer.setBackground(Color.white);
				
				newPlayer.setEnabled(true);
				newNumber.setEnabled(true);
				theGuess.setEnabled(true);
				
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				
			}
		}
		
		public void updateScore()
		{
			thePlayer.setText(playerName);
			
			bankRoll.setText(amtRemaining + " Pounds");
			
			
			
		}
		
		public void displayInstructions()
		{
			theOutput.setText("");
			theOutput.append("Instructions: \nEnter a randon number");
			
		}
		
		
		public void gameWon()
		{
			theOutput.setText("");
			
			double curWinning;
			
			switch (numTries)
			{ 
				case 1:
					theOutput.append("********** WINNER **********");
					curWinning = + 2.00;
					theOutput.append("\n" + "You have won: 2.00 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break; 
					
				case 2:
					theOutput.append("********** WINNER **********");
					curWinning = + 1.75;
					theOutput.append("\n" + "You have won: 1.75 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 3: 
					theOutput.append("********** WINNER **********");
					curWinning = + 1.50;
					theOutput.append("\n" + "You have won: 1.50 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 4:
					theOutput.append("********** WINNER **********");
					curWinning = + 1.25;
					theOutput.append("\n" + "You have won: 1.25 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 5: 
					theOutput.append("********** WINNER **********");
					curWinning = + 1.00;
					theOutput.append("\n" + "You have won: 1.00 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 6:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.75;
					theOutput.append("\n" + "You have won: 0.75 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 7:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.50;
					theOutput.append("\n" + "You have won: 0.50 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 8:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.25;
					theOutput.append("\n" + "You have won: 0.25 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				default:
					theOutput.append("********** LOSER **********");
					curWinning = + 0.00;
					theOutput.append("\n" + "Unlucky you have not won!" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
			}
			
			playerName = thePlayer.getText();
			theOutput.append("\n" + "Current Player: " + playerName + " " + "\n" +"Amount Won: " + amtRemaining + " Pounds");
			
			theOutput.append("\nPress New Number to continue");
			
			updateScore();
			
			theGuess.setBackground(Color.white);
			theGuess.setEnabled(false);
			newNumber.requestFocus(true);
			
		}
		
	}
[+][-]02/24/09 04:34 AM, ID: 23720720Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/24/09 04:34 AM, ID: 23720722Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]02/24/09 04:38 AM, ID: 23720743Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: New to Java Programming
Sign Up Now!
Solution Provided By: arioh
Participating Experts: 3
Solution Grade: A
 
[+][-]02/24/09 04:38 AM, ID: 23720744Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_3_20080625