Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

neelvaka

macrumors newbie
Original poster
Sep 15, 2007
16
0
Code:
//I'm going to use these from now own. Help organize thoughts more for me.

import java.io.*;

        public class quadratic {
        	 
        	
        	
        public static void main(String args[])

        throws IOException {

                BufferedReader IN = new BufferedReader (new InputStreamReader(System.in));
		int x = (int)(100 * Math.random()) + 1; 
		int inputnumber = 0; //this is the user input #
		
		int counter = 0;
		double N2, z2;
			
		   String N;
	       System.out.print("Say 1 To Start Game, and 2 To Quit:\t");
	       N = IN.readLine ();
	       N2 = Integer.parseInt (N);

			while (N2 == 2)
				{
				
					System.out.println("Goodbye!");
					break;
					
				}
			while (N2 == 1)
				{
					
					System.out.println("\nI am thinking of a number between 1 and 100. Try to guess it.\n"); 

					
						String z;
						System.out.print("What's your guess? ");
						z = IN.readLine ();
						z2 = Integer.parseInt (z);
						{
						
						if (z2 > x)
						{
							System.out.println(z2 + " is too big!\n");
						}

						else if (z2 < x)
						{
							System.out.println(z2 + " is too small!\n");
						}
						counter = counter + 1;
					}
					if (counter == 1)
					{
						System.out.println("You've got it in " + counter + " guesses. That was lucky!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
					else if (counter >= 2 && counter <= 4)
					{
						System.out.println("You've got it in " + counter + " guesses. That was amazing!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
					else if (counter >= 5 && counter <= 6)
					{
						System.out.println("You've got it in " + counter + " guesses. That was really good!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
					else if (counter == 7)
					{
						System.out.println("You've got it in " + counter + " guesses. That was ok!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
					else if (counter >= 8 && counter <= 9)
					{
						System.out.println("You've got it in " + counter + " guesses. That was pretty bad!");
						System.out.print ("\n \n\n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
					else
					{
						System.out.println("You've got it in " + counter + " guesses. This is not your game!");
						System.out.print ("\n \n \n \t \t \t \t \t \t \t \t \t \t BY Neel Vakharia");
						break;
					}
				}
				}
	}

Thats my code
it works
but everytime i run it it duz wat i dotn want it to do
the number gets chosen randomly BUT when the user takes a guess at the number it says whether its to big or small rite BUT it ends it rite there and then it doesnt stop when the user guesses it rite it stops on the users first reply:confused:


btw: ECLIPSE PLATFORM is used to run these scripts
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
A couple things,

1) The open curly brace '{' below the line "z2 = Integer.parseInt (z);" and the close one above "if (counter == 1)" are pointless. They do nothing.

2) The reason it stops after one pass is because you will always hit a break statement the way you have things written. Rethink your loops. You'll need a separate loop for the guessing before it reaches the code for displaying how many guesses it took.

3) There should be a way for a person to leave the guessing, maybe have them type 'q' to give up and quit. Since it only runs once, having a menu of options at the beginning is pointless, unless you set things up to return the menu after they guess the number right. It may have been a requirement for the assignment though so do whatever the prof asks of course.

Hope that gets you in the right direction.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
A couple things,

1) The open curly brace '{' below the line "z2 = Integer.parseInt (z);" and the close one above "if (counter == 1)" are pointless. They do nothing.

They actually affect the scope of anything defined in that block, so they don't do "nothing".

For example:
Code:
class ScopeTest
{
	public static void main()
	{
		{
			int x = 0;
		}
		
		System.out.println(x);
	}
}

Try building that and you get this error:

$ javac ScopeTest.java
ScopeTest.java:9: cannot find symbol
symbol : variable x
location: class ScopeTest
System.out.println(x);
^
1 error

2) The reason it stops after one pass is because you will always hit a break statement the way you have things written.
I said this above, but more indirectly.

3) There should be a way for a person to leave the guessing, maybe have them type 'q' to give up and quit.
Quitters never win. :p
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Here's a quick and dirty example that I threw together:

Code:
import java.util.Random;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.Math;

class Guess
{
  public static void main(String args[])
  {
    try
    {
      // initialize the flag to break the loop.
      boolean done = false;

      // setup the input parser.
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

      // setup a RNG (random number generator)
      // and seed it w/ the current time.
      Random RNG = new Random(System.currentTimeMillis());

      System.out.println("\nI'm choosing a number between 1 and 10 ... What is it?");

      while(done == false)
      {
        // initialize the input and target values.
        int inputValue  = 0;
        int targetValue = Math.abs(RNG.nextInt() % 10);
        
        // initialize the flag to break the loop.
        boolean numGuessed = false;
        
        while(numGuessed == false)
        {
          inputValue = Integer.parseInt(input.readLine());

          if(inputValue == targetValue)
          {
            System.out.println("\nBingo!  I'm choosing another number ... What is it?");
            numGuessed = true;
          }

          else
          {
            if(inputValue > targetValue)
            {
              System.out.println("\nYou're too high.  Try again.");
            }

            if(inputValue < targetValue)
            {
              System.out.println("\nYou're too low. Try again.");
            }
          }
        }
      }
    }
    
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

There's no quit logic, but it works.
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
Though you're not supplying a complete solution, be careful not to help people working on homework too much, otherwise they learn nothing.

I appreciate the caution, but I did that code in five minutes. I learned a lot in my early stages of programming by looking at short examples and other code snippets.

If he copies and pastes it, that's his fault. Not mine.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.