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.

New Topic/Question
Reply



MultiQuote




|