Click here to Skip to main content
15,908,906 members
Home / Discussions / Java
   

Java

 
AnswerRe: JTabel Cell Pin
David Skelly24-Oct-10 22:16
David Skelly24-Oct-10 22:16 
QuestionNeed help with this code [modified] Pin
zooropean720-Oct-10 16:33
zooropean720-Oct-10 16:33 
AnswerRe: Need help with this code Pin
Bugclear20-Oct-10 21:32
Bugclear20-Oct-10 21:32 
GeneralRe: Need help with this code Pin
Richard MacCutchan20-Oct-10 22:10
mveRichard MacCutchan20-Oct-10 22:10 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 8:33
zooropean721-Oct-10 8:33 
GeneralRe: Need help with this code Pin
Richard MacCutchan21-Oct-10 11:21
mveRichard MacCutchan21-Oct-10 11:21 
GeneralRe: Need help with this code Pin
zooropean721-Oct-10 12:16
zooropean721-Oct-10 12:16 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 20:31
Bugclear21-Oct-10 20:31 
Hi guys,
I edited the code a little more yesterday. The logic behind "guess=101" are the following reasons.
1:When the user gives the correct answer or when the user chooses "0" to quit the variable has to be reset to some value, in this case i used "101" but now i ve made a small edit and changed it "guess = MAX+1", just to make the program a little more dynamic.
2:Also, i have included a condition saying if "guess = MAX+1" then the game restarts again prompting the user to guess a number.
Im pasting the edited code below for your reference. I believe now the program is bug clear. I have fun playing Smile | :)
import java.util.*;
public class Guess
{
	public static void main (String[] args)
	{
		final int MAX = 100;
		int answer, guess, attempt = 0;
		String replay = "Y";
		
		Scanner scan = new Scanner (System.in);
		Random generator = new Random();
		answer = generator.nextInt(MAX) + 1;
		System.out.print("I am thinking of a number "
						+ "between 1 and "+ MAX+". Enter your guess (0 to guit):");
		guess = scan.nextInt();
		
		do 
		{	
			if(guess == MAX + 1){

				answer = generator.nextInt(MAX) + 1;
				
				System.out.print("I am thinking of a number "
								+ "between 1 and "+ MAX+". Enter your guess (0 to guit):");
				guess = scan.nextInt();
			}
			
			if (guess == 0)
			{
			System.out.println ("You ended the game. Play again? (y/n):");
			replay = scan.next();
			guess = MAX + 1;
			attempt = 0;
			}
			else{
				if (guess > answer)
				{
					attempt++;
					System.out.print ("Try a smaller number (0 to quit): ");
					guess = scan.nextInt();
				}	
			}
			if (guess == 0)
			{
			System.out.println ("You ended the game. Play again? (y/n):");
			replay = scan.next();
			guess = MAX + 1;
			attempt = 0;
			}
			else{
				if (guess < answer)
				{
					attempt++;
					System.out.print ("Try a bigger number (0 to quit): ");
					guess = scan.nextInt();
				}
			}
		
			if (guess == answer)
			{
			attempt++;
			System.out.println ("You got it!");
			System.out.println ("Number of total attempts:" + attempt);
			System.out.println ();
			System.out.print("Play again? (y/n)");
			replay = scan.next();
			guess = MAX + 1;
			attempt = 0;
			}
			
		}while (replay.equals("y") || replay.equals("Y"));
		
		System.out.println("\nThanks for playing - GUESS THE NUMBER");
		
	}
}

GeneralRe: Need help with this code Pin
TorstenH.21-Oct-10 22:52
TorstenH.21-Oct-10 22:52 
GeneralRe: Need help with this code Pin
Bugclear21-Oct-10 23:22
Bugclear21-Oct-10 23:22 
GeneralRe: Need help with this code Pin
Richard MacCutchan22-Oct-10 3:05
mveRichard MacCutchan22-Oct-10 3:05 
QuestionException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 0:19
vidhyaej20-Oct-10 0:19 
AnswerRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 0:56
TorstenH.20-Oct-10 0:56 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:03
vidhyaej20-Oct-10 1:03 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
TorstenH.20-Oct-10 1:19
TorstenH.20-Oct-10 1:19 
GeneralRe: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Pin
vidhyaej20-Oct-10 1:29
vidhyaej20-Oct-10 1:29 
QuestionClasses and methods Pin
vidhyaej19-Oct-10 21:28
vidhyaej19-Oct-10 21:28 
AnswerRe: Classes and methods Pin
Nagy Vilmos19-Oct-10 22:01
professionalNagy Vilmos19-Oct-10 22:01 
GeneralRe: Classes and methods Pin
vidhyaej19-Oct-10 22:58
vidhyaej19-Oct-10 22:58 
GeneralRe: Classes and methods Pin
Richard MacCutchan19-Oct-10 23:23
mveRichard MacCutchan19-Oct-10 23:23 
GeneralRe: Classes and methods Pin
vidhyaej19-Oct-10 23:41
vidhyaej19-Oct-10 23:41 
GeneralRe: Classes and methods Pin
TorstenH.19-Oct-10 23:27
TorstenH.19-Oct-10 23:27 
GeneralRe: Classes and methods Pin
vidhyaej19-Oct-10 23:43
vidhyaej19-Oct-10 23:43 
QuestionPaint problem in java Pin
tamash_ionut19-Oct-10 13:01
tamash_ionut19-Oct-10 13:01 
AnswerRe: Paint problem in java Pin
Nagy Vilmos19-Oct-10 22:03
professionalNagy Vilmos19-Oct-10 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.