Question:
Java random number game?
thetree718
2009-04-24 11:57:47 UTC
So I am writing a game where the computer chooses a random number then the user tries to guess. This code just doesn't want to work. any help would be nice, thanks.

also im aware that the "int n1= generator..." gives me the error n1 is defined in main(java.lang.string[]). i dont know how to fix it.

import java.util.Scanner;
import java.util.Random;

public class gamez
{
public static void main(String args[])
{
int n1, n2, n3, n4, choice = 1;

System.out.println("I want to play a game...");

while (choice == n3)
{
int n1 = generator.nextInt(100) + 1;

System.out.println("What number am I computing of?(1-100)");
n2 = input.nextInt();

if(n2 == n1)
{
System.out.println("Man you are good!");
}
else if (n2 > n1)
{
System.out.println("Thats too high, guess lower");
}
else if (n2 < n1)
{
System.out.println("Thats too low, guess higher");
}
System.out.println("Game Over, Good Job");
System.out.println("Play again? (Yes = 1, No=2)");
n3 = input.nextInt();
if(n3 == 1)
{
}
{
System.out.println("Aww ok :P");
}

System.out.println("Good game, join me again later and I'll compute a new number just for you");
}
}
}
Three answers:
deonejuan
2009-04-24 12:21:44 UTC
import java.util.Scanner;

import java.util.Random;



public class HighLowGame {







public static void main(String[] args) {



boolean done = false;



// for testing, could start out with a random no. however

int aRandomNo = 44;



Scanner sc = new Scanner(System.in);



while (!done) {



System.out.println("I'm thinking of a number between 1 and 100");

System.out.print("What do you think it is? ==> ");

Integer guess = Integer.parseInt( sc.nextLine() );



int situation = guess.compareTo( aRandomNo );

// compareTo gives a -1, 0, 1

if( situation == 0) {

System.out.println("OMG!!! you hit the bull's-eye");

System.out.println("This game is about over");

System.out.println("I picked " + aRandomNo);

System.out.println("And you hauled off and guessed it ... " + guess );

System.out.println("YOU don't need spare time, you've got HIGH_l-o-o-o-o");

System.out.print("Would Mr. HiLo Expert like to do this again? [y/n] ==> ");

String answer = sc.nextLine();

if( ! answer.equalsIgnoreCase( "n" ) {

done = false;

Random r = new Random();

aRandomNo = r.nextInt(100) + 1;

System.out.println("A whole NEW number...");

} else {



done = true;

System.out.println("Sometimes you win a few. Sometimes you lose a few.");

System.out.println("Adios, and thanks for all the fish!");

}



}

if( situation > 0 ) {

System.out.println("You are << high >>");

System.out.println("I understand Kmart has \'Get A Life\' on sale for $14.95");



}

if( situation < 0 ) {

System.out.println("Too << low >>");

System.out.println("Is your resum\u00E9 current with 7/11?");

}

}

}









}
J.T.
2009-04-24 12:14:40 UTC
Glancing at it quickly I've noticed a number of problems.

-The first problem I've noticed is that you aren't declaring n1, n2, n3, or n4 as anything. Because you're doing this when it gets to while (choice == n3) n3 is equal to null at that moment. It appears, however, that you intend to take in user input to set n3 so once you are actually taking in input you should be fine.



-The second problem, relates to your error n1 is defined in main(java.lang.string[]) issue. The reason you are getting this error is because when you are using the line int n1 = generator.nextInt(100) + 1;

you are overwriting the instance of n1 that you created five lines above it (int n1, n2, n3, etc.). What you need to do to fix it is rather than saying: int n1 = generator.nextInt(100) + 1; instead say n1 = generator.nextInt(100) + 1;



After that you should be in the clear. Of course, the next two things you need to do is take user input and create your random generator so that the variables generator and input aren't null but I'm sure you were going to do that. Hope this was helpful.
BeerMeQuik
2009-04-24 12:09:32 UTC
first

Class names should be capitalized.



public class Gamez





You already defined the variable n1 in the main function. You cannot redeclare it within the same function.



Your statement should be

n1 = generator.nextInt(100) + 1;





I see some other logic errors (or some code got cut out) but I will let you find them.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...