Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2012, 02:39 PM   PM User | #1
Patrik116
New to the CF scene

 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Patrik116 is an unknown quantity at this point
while y/n

PHP Code:
    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?
Patrik116 is offline   Reply With Quote
Old 02-14-2012, 03:16 PM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
you never call this again
Code:
int number = generator.nextInt(100) + 1;
so the number is the same

Quote:
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
Code:
Tries++;
break;
that will break on "break;"
confused as to question or purpose....
I would do two loops...
Code:
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;
}
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Reply

Bookmarks

Tags
java

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:24 AM.


Advertisement
Log in to turn off these ads.