...

View Full Version : while y/n



Patrik116
02-14-2012, 02:39 PM
import java.util.Random;
import java.util.Scanner;
class indexerad_variable
{
public static void main(String[] args)
{

Random generator = new Random();
Scanner scan = new Scanner(System.in);

int Tries=0;
int number = generator.nextInt(100) + 1;
String igen;

while(true)
{
System.out.print("\nNumber 1 - 100: ");
Tries++;
int guess = scan.nextInt();

if(guess < number)
{
System.out.println("to low");
}

if(guess > number)
{
System.out.println("to high");
}

if(guess == number)
{
System.out.println("Right you have tried "+Tries+" ");
{
}
System.out.print("\nPlay again y/n: ");
igen = scan.next();
if (igen.equalsIgnoreCase("N"))
break;
}
}
}
}
The program works but when i press y it will com back to the last game. and if i type in a break under tries its only go 1 turn in the game. can anyone tell me what i need to do to fix this game?

alykins
02-14-2012, 03:16 PM
you never call this again


int number = generator.nextInt(100) + 1;

so the number is the same



and if i type in a break under tries its only go 1 turn in the game.

I don't understand what you are trying to do or are saying... you mean


Tries++;
break;

that will break on "break;"
confused as to question or purpose....
I would do two loops...


bool GameDone=false;
bool CorrectInt=false;
while(!GameDone)
{
initialize all your variables...
while(!CorrectInt)
{
code for game
on correct int guess set CorrectInt = true;
}
Ask if want to play again
on n set GameDone = true;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum