6 Replies - 439 Views - Last Post: 28 February 2012 - 12:42 AM Rate Topic: -----

#1 hellswindstaff  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-February 12

random number generator println problem?!?!?

Posted 27 February 2012 - 06:59 PM

/* For some reason the System.out.println statements for all of the "else if"
statements will not be displayed.
**/


/*This is a game in which the user guesses 
  which number the random number generator created. 
**/

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

class ch4no17
{
	public static void main(String[] args) {
		Random ranNum = new Random();// random number
		int number;
		int numInput = 0;
		number = ranNum.nextInt(25);//guess between 0-25	
		Scanner numIn = new Scanner(System.in);//allows user to input number
		numInput = numIn.nextInt();//sets user input equal to number
		System.out.println(numIn.nextLine());//displays user input
				
 
	//pre-test loop which tests to see if user has guess the 
	//randomly generated number correctly

		while (numInput != number)
			{
			numInput = numIn.nextInt();
			}

		if (numInput == number)
			System.out.println("Congratulations! You got it right!");

		else if (numInput < number)
			System.out.println("Too low, try again");
			
		else if (numInput > number)
			System.out.println("Too high, try again");
	
		}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: random number generator println problem?!?!?

#2 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: random number generator println problem?!?!?

Posted 27 February 2012 - 07:01 PM

Y do u have a .nextLine in there? just have a System.out.println(numIn);
Was This Post Helpful? 0
  • +
  • -

#3 exiles.prx  Icon User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 241
  • Joined: 22-November 10

Re: random number generator println problem?!?!?

Posted 27 February 2012 - 07:22 PM

I bet your code is halting here because its waiting for an input value.
while (numInput != number) {
 numInput = numIn.nextInt();
 }


EDIT: and it will not exit the while loop until the number you input is equal to number.

This post has been edited by exiles.prx: 27 February 2012 - 07:25 PM

Was This Post Helpful? 1
  • +
  • -

#4 hellswindstaff  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-February 12

Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:01 PM

I have an value for numInput it is 0. It's at the top.

.nextLine is suppose to be there, but I would have to show you a visual for the program for it to be obvious.

The thing it is running fine. Everything is compiling. It's just not showing;

"Too low, try again"
or
"Too high, try again"


while it is running.
Was This Post Helpful? 0
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:14 PM

What exiles is saying is that:

Lets say your number is 42.
user enters 41, loop tries again
user enters 39, loop tries again
user enters 42, the condition becomes false exiting the loop.

while (numInput != number) {
 numInput = numIn.nextInt();
}



now, you check to see if the numInput is equal to number, which will always happen since you made your loop forces the value to be equal to the number.

This post has been edited by Mylo: 27 February 2012 - 11:15 PM

Was This Post Helpful? 1
  • +
  • -

#6 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: random number generator println problem?!?!?

Posted 27 February 2012 - 11:15 PM

I'm confused, why are your "else if" statements not within the while loop?

Don't you want it to run until you guess correctly? If you keep asking them for an input over and over until they get it right, then of course the statements will never be printed. Those statements need to be inside the while loop.
Was This Post Helpful? 1
  • +
  • -

#7 hellswindstaff  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-February 12

Re: random number generator println problem?!?!?

Posted 28 February 2012 - 12:42 AM

Wow... okay I see now. Careless error.

ty guys/ gals!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1