2 Replies - 267 Views - Last Post: 22 April 2012 - 05:01 AM Rate Topic: -----

#1 derrickcheung  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-April 12

need help ,debug ,please !

Posted 22 April 2012 - 04:46 AM

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


public class question3
{

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub


		int guess ,number,count=0;
		String str ;
		boolean result =false;
		boolean yield =false;
		Scanner scan =new Scanner (System.in) ;
		Random ran= new Random () ;

		while (!yield)
		{
			guess=ran.nextInt(99)+1;
			if (count==0)
				System.out.println("Play? (Y/N)");
			else
				System.out.println("Play again? (Y/N)");

			str=scan.next();
			if ("y".equalsIgnoreCase(str)) 
			{
				System.out.println("Cheat: target ="+guess);
				System.out.println("Guess the target integer which is between 1 and 100 inclusive :");
				number=scan.nextInt();
				while (!result) 
				{
					if (number<(guess-5)||number>(guess+5))
					{
						System.out.println("Guess again");
						number=scan.nextInt(); 
					}
					else if (number==guess)
						{
							System.out.println("You Guessed it!");
							result = true ;
						}
					else
						{
							System.out.println("Close enough, do you want to keep guessing? (Y/N)");
							
							str=scan.next();


							if ("y".equalsIgnoreCase(str))
							{
								while (!result)
								{
									System.out.println("Guess again ");
									number=scan.nextInt();
									if (number==guess)
									{
										System.out.println("You Guessed it!");
										result = true ;
									}
								}
							}
							else 
								result = true;
						}
					
				}
				count=1;

			}
			else
				yield=true ;

		}

		



	}


}




help! when i input an integer ,preceded by " Guess the target integer which is between 1 and 100 inclusive :" for the second time (assume i have run the program once ), why it jump to "Play again? (Y/N)" ,rather than "Close enough, do you want to keep guessing? (Y/N)" or "You Guessed it!" or "Guess again" like the first time i run it? what is wrong with my code ?

This post has been edited by Atli: 22 April 2012 - 07:51 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: need help ,debug ,please !

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: need help ,debug ,please !

Posted 22 April 2012 - 05:01 AM

Gentle poster,

Welcome to DIC, but please post your code in code tags.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: need help ,debug ,please !

Posted 22 April 2012 - 05:01 AM

Please use [code] tags as instructed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1