# Some weird problem PLZ HELP (im a beginner so i think the problem is a beginner prob)

Discussion in 'Mac Programming' started by neelvaka, Sep 16, 2007.

1. ### neelvaka macrumors newbie

Joined:
Sep 15, 2007
#1
Code:
```//I'm going to use these from now own. Help organize thoughts more for me.

import java.io.*;

public static void main(String args[])

throws IOException {

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");
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;
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

btw: ECLIPSE PLATFORM is used to run these scripts

2. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#2
Try using 'continue' instead of 'break'.

There are far better ways to do what you're doing, but if it works that's fine.

3. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#3
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.

4. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#4

5. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#5
Here's a quick and dirty example that I threw together:

Code:
```import java.util.Random;
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.

// 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)
{

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.

6. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#6
Though you're not supplying a complete solution, be careful not to help people working on homework too much, otherwise they learn nothing.

7. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#7
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.