Staredit Network

Staredit Network -> Computers and Technical -> Damn Java
Report, edit, etc...Posted by Mp)7-7 on 2006-11-03 at 22:29:55
Ok, I am making a hi_lo game where the computer generates a random number, which you have to guess, everytime you guess it tells you if you are low or higher than the number. Once you reach it, it tells you how many guesses it took and if you want to play again. I have a while loop to go back to it, I cant get it to work.

Here is my code.
CODE

import java.util.Scanner;
public class hi_lo
{
   public static void main(String[] args)
   {
   Scanner scan = new Scanner(System.in);
   
   int answer, guess = 1455486, count=1, yn=1;
   
   answer = (int)(Math.random() * 100 + 1);
   System.out.println ("Guess a number between 1 and 100, 0 to quit");
   
   guess = scan.nextInt();
       {
       while (yn == 1)
           {
           while (guess != 0 && guess != answer)
           {
               count++;
               if (guess < answer)
               System.out.println ("Your to low, guess again or zero to quit ");
           else
                   System.out.println ("Your to high, guess again or zero to quit ");
               guess = scan.nextInt();
           }
           if (guess == answer)
               {
               System.out.println ("You guessed it right! It took you " + count++ + " guesses! Do you want to play again? 1 for yes, 0 for no");
               }
           yn = scan.nextInt();
           }
       }
   }
}


Another way to look at it.
CODE

import java.util.Scanner;
public class hi_lo
{
   public static void main(String[] args)
   {
   Scanner scan = new Scanner(System.in);
   
   int answer, guess = 1455486, count=1, yn=1;
   
   answer = (int)(Math.random() * 100 + 1);
   System.out.println ("Guess a number between 1 and 100, 0 to quit");
   
   guess = scan.nextInt();
       {
       while (yn == 1)
           {
           while (guess != 0 && guess != answer)
           {
               count++;
               if (guess < answer)
               System.out.println ("Your to low, guess again or zero to quit ");
           else
                   System.out.println ("Your to high, guess again or zero to quit ");
               guess = scan.nextInt();
           }
           if (guess == answer)
               {
               System.out.println ("You guessed it right! It took you " + count++ + " guesses!");
               }
           System.out.println ("Do you want to play again? 1 for yes, 0 for no.");
           yn = scan.nextInt();
           }
       }
   }
}
Report, edit, etc...Posted by Voyager7456(MM) on 2006-11-03 at 22:42:01
Hmmm... in my Java class we use a different method to read in variables... is this part of an assignment (do you have to use that method?)
Report, edit, etc...Posted by Mp)7-7 on 2006-11-03 at 22:46:28
i dont know what you mean but ya this is for my class and grades are due today, without this done I may get under a B which I think will be the only grade keeping my off the honor-roll which I kinda want, so if anyone could help me out that would be great. I am confused, it just keeps telling me:

Do you want to play again? 1 for yes, 0 for no.
1
Do you want to play again? 1 for yes, 0 for no.
1
Do you want to play again? 1 for yes, 0 for no.
0

This is what it looks like when I am done.
Report, edit, etc...Posted by Vibrator on 2006-11-03 at 22:58:49
CODE

import java.util.Scanner;
public class hi_lo
{
  public static void main(String[] args)
  {
  Scanner scan = new Scanner(System.in);
 
  int answer, guess = 1455486, count=1, yn=1;
 
  answer = (int)(Math.random() * 100 + 1);
  System.out.println ("Guess a number between 1 and 100, 0 to quit");

  guess = scan.nextInt();
     
      while (yn == 1)
          {
          while (guess != answer && guess != 0)
          {
              count++;
              if (guess < answer)
              System.out.println ("Your to low, guess again or zero to quit ");
             
          else
              System.out.println ("Your to high, guess again or zero to quit ");
              guess = scan.nextInt();
          }
          if (guess == answer)
              {
              System.out.println ("You guessed it right! It took you " + count++ + " guesses! Do you want to play again? 1 for yes, 0 for no");
             
              }
           if (guess == 0) System.out.println("Are you sure you want to quit? (one for no)");
           yn = scan.nextInt();
           if (yn==1){
            answer = (int)(Math.random() * 100 + 1);
            System.out.println ("Guess a number between 1 and 100, 0 to quit");
      count = 0;
      guess = scan.nextInt();
           }
 
          }
     
  }
}
Report, edit, etc...Posted by Mp)7-7 on 2006-11-03 at 23:11:53
OMG thank you so much, I think it was you that helped me last time too. Well thanks again. I think I will just PM you when I have another problem.
Report, edit, etc...Posted by Vibrator on 2006-11-03 at 23:12:53
If you want one that doesnt ask twice u can use this tongue.gif
CODE

import java.util.Scanner;
public class hi_lo
{
  public static void main(String[] args)
  {
  Scanner scan = new Scanner(System.in);
 
  int answer, guess = 1455486, count=1, yn=1;
 
  answer = (int)(Math.random() * 100 + 1);
  System.out.println ("Guess a number between 1 and 100, 0 to quit");

  guess = scan.nextInt();
     
      while (guess > 0 && yn == 1)
          {
          while (guess != answer && guess != 0)
          {
              count++;
              if (guess < answer)
              System.out.println ("Your to low, guess again or zero to quit ");
             
          else
              System.out.println ("Your to high, guess again or zero to quit ");
              guess = scan.nextInt();
          }
          if (guess == answer)
              {
              System.out.println ("You guessed it right! It took you " + count++ + " guesses! Do you want to play again? 1 for yes, 0 for no");
              yn = scan.nextInt();
              }
           
           
           if (yn==1){
            answer = (int)(Math.random() * 100 + 1);
            System.out.println ("Guess a number between 1 and 100, 0 to quit");
      count = 0;
      guess = scan.nextInt();
           }
 
          }
     
  }
}
Report, edit, etc...Posted by Mp)7-7 on 2006-11-03 at 23:21:32
Ya, I revised it, I already sent it and fixed it actually it looked a lot like that. Dont you think I have come along way since last time I pretty much asked you for the whole thing. lol
Report, edit, etc...Posted by Kow on 2006-11-17 at 02:13:23
We must be on the same curriculum because I made one of those programs too

Edit: Also, if you have any problems with any of the programs, chances are I'll know what to do, so if you don't wanna make a new proggy, you can just IM me or email me.
Next Page (1)