1.

Solve : Guessing Game Program [JAVA]?

Answer»

I made a program that generates a random integer between 0 and 100, asks the user to guess it through console, and prints out results accordingly. I made use (most likely over use) of if and else if (methods? What do you call them?) and was wondering if what I have punched out is acceptable and efficient or unreadable spaghetti code.

package numberguessinggame2;
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args) {

INT rdmint = 0;
int guess = 0;
while (rdmint == 0){
rdmint = (int)(Math.random()*101);
}

Scanner input = new Scanner(System.in);
while(guess != rdmint){
System.out.print("Enter an interger between 0 and 100: ");
String userinput = input.nextLine();
try {
guess = Integer.parseInt(userinput);
} catch (NumberFormatException e) {
}
boolean range = ((guess < 100) && (guess > 0));
if ((guess == rdmint) && range){
System.out.println("Your guess is correct");
}
else if ((guess > rdmint) && range){
System.out.println("Your guess is too high");
}
else if ((guess < rdmint) && range){
System.out.println("Your guess is too low");
}else {
System.out.println("Your guess is invalid");
}
}
}
}

Why not try something like NetBeans and run it. You'll soon find out if it works.

http://netbeans.org/Quote from: reddevilggg on June 09, 2012, 08:12:34 AM

Why not try something like NetBeans and run it. You'll soon find out if it works.

http://netbeans.org/

The program seems to run correctly. I'm more concerned with it's complexity. Am I following the correct style or reinventing the wheel?My tutor always told me "if it works, it works". Ask 10 people to do the same thing and you will probably get 10 different answers. Experience and practice will allow you to streamline code, as you progress.

Example.
Q: What 2 numbers, when ADDED, equal 10?
A: 9+1 or 8+2 or 7+3 etc

Also, test the boundries...
Quote
I made use (most likely over use) of if and else if (methods? What do you call them?
Control structures.

The code is not indented/formatted well, but I think that might be because it was placed into the post and not in a separate code tag:

Code: [Select]package numberguessinggame2;
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args) {
int rdmint = 0;
int guess = 0;

while (rdmint == 0){
rdmint = (int)(Math.random()*101);
}

Scanner input = new Scanner(System.in);
while(guess != rdmint){

System.out.print("Enter an interger between 0 and 100: ");
String userinput = input.nextLine();
try {guess = Integer.parseInt(userinput);} catch (NumberFormatException e) {}
boolean range = ((guess < 100) && (guess > 0));

if ((guess == rdmint) && range){
System.out.println("Your guess is correct");
}
else if ((guess > rdmint) && range){
System.out.println("Your guess is too high");
}
else if ((guess < rdmint) && range){
System.out.println("Your guess is too low");
}else{
System.out.println("Your guess is invalid");
}

}
}
}

another common idiom is to label the closing brace for the larger constructs. (the second while here, the main method, and the class, for instance).

You could also replace the try..catch logic with something a bit simpler, as well as more functional:

eg. instead of:
Code: [Select]System.out.print("Enter an interger between 0 and 100: ");
String userinput = input.nextLine();
try {guess = Integer.parseInt(userinput);} catch (NumberFormatException e) {}
boolean range = ((guess < 100) && (guess > 0));

Code: [Select]do {
System.out.print("Enter an integer between 0 and 100: ");
if(scanner.hasNextInt()) x = scanner.nextInt();
else scanner.next();
} while (guess < 0 || guess > 100);
which would MAKE the use of "range" in the conditionals lower down redundant, since by that point the number has been guaranteed to be in that range by the input loop.
I put in the hasNextInt method you put down as well as removed the range variable and tried to adjust the spacing of the brackets accordingly. Does the hasNextInt method (for lack of a BETTER word) initialize the scanner?

Code: [Select]package numberguessinggame2;
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args) {
// Initialize variables and scanners
int rdmint = 0;
int guess = 0;
Scanner input = new Scanner(System.in);
String invalidEntry = "Invalid entry.";

//GENERATE a random integer between 0 and 100
while (rdmint == 0){
rdmint = (int)(Math.random()*101);
}

//Take in a guess from user and determine if it is a valid entry. Then determine if it is the correct entry. Display results to console.
while ((guess <= 0) || (guess >= 100) || (guess != rdmint)){
System.out.print("Enter an integer between 0 and 100: ");
if((input.hasNextInt())){
guess = input.nextInt();
if (guess < 100 && guess > 0){
if (guess == rdmint){
System.out.println("Your guess is correct");
}
if (guess < rdmint){
System.out.println("Your guess is too low");
}
if (guess > rdmint){
System.out.println("Your guess is too high");
}
}
else{
System.out.println(invalidEntry);
}
}
else{
System.out.println(invalidEntry);
input.next();
}
}
}
}


Discussion

No Comment Found