# Missing loop in main function

Page 1 of 1

## 3 Replies - 992 Views - Last Post: 01 December 2008 - 05:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=74444&amp;s=7183c784cec510524645ca2926d11727&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 grayzer

Reputation: 0
• Posts: 19
• Joined: 03-November 08

# Missing loop in main function

Posted 01 December 2008 - 03:29 PM

```//Write a program which asks the user for 1 integer number between 1 and 10.
//Get the computer to randomly generate a number between 1 and 10
//If your number == it's number, print "you have won"
//If your number < random number, print "Too low"
//if your number > random number, print "Too High"
//Print out both numbers

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

public class Game
{
public static Random generator = new Random();
private static Scanner s = new Scanner(System.in);

public static void main(String args[])
{
int numberOne, numberTwo;

// Generate a random number
numberTwo = generator.nextInt(10);
System.out.println("Random number is " + numberTwo);

// Prompt the user for a number
numberOne = s.nextInt();
System.out.println("Your number is " + numberOne);

// Check guess against random number
checkAgainstRandomNumber(numberOne, numberTwo);
}

private static void checkAgainstRandomNumber(int num1, int num2)
{

// Compare the numbers
if (num1 == num2) {
System.out.println("You have won");
}
else if (num1 > num2) {
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Missing loop in main function

### #2 Martyr2

• Programming Theoretician

Reputation: 5201
• Posts: 13,944
• Joined: 18-April 07

## Re: Missing loop in main function

Posted 01 December 2008 - 03:39 PM

Oh come on grayzer, I have done half of the assignment for you already and you didn't even TRY to produce a loop in your main function. Look in your book or on the net of how a loop is constructed, put it in the main function around where you ask for user input and check it against the random number and you will see you can probably set it up in about 15 - 20 minutes.

Give it a try at least and show us what you come up with. If you run into troubles that is what we are here to help for.

### #3 grayzer

Reputation: 0
• Posts: 19
• Joined: 03-November 08

## Re: Missing loop in main function

Posted 01 December 2008 - 03:56 PM

Martyr2, on 1 Dec, 2008 - 02:39 PM, said:

Oh come on grayzer, I have done half of the assignment for you already and you didn't even TRY to produce a loop in your main function. Look in your book or on the net of how a loop is constructed, put it in the main function around where you ask for user input and check it against the random number and you will see you can probably set it up in about 15 - 20 minutes.

Give it a try at least and show us what you come up with. If you run into troubles that is what we are here to help for.

Hi Martyr2,
i've been trying different loops all evening and I just can't get my head around it - I didn't realise I uploaded the program without the loop I put in. Have look and see what you think cos I'm probably going off in the wrong direction.

Thanks, grayzer.

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

public class Game
{
public static Random generator = new Random();
private static Scanner s = new Scanner(System.in);

public static void main(String args[])
{

int i = 0;
while (i < 10)
{
i++;

{
int numberOne, numberTwo;

// Generate a random number
numberTwo = generator.nextInt(10);
System.out.println("Random number is " + numberTwo);

// Prompt the user for a number
numberOne = s.nextInt();
System.out.println("Your number is " + numberOne);

// Check guess against random number
checkAgainstRandomNumber(numberOne, numberTwo);
}

private static void checkAgainstRandomNumber(int num1, int num2)
{

// Compare the numbers
if (num1 == num2) {
System.out.println("You have won");
}
else if (num1 > num2) {
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
}
}
}

```

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Missing loop in main function

Posted 01 December 2008 - 05:13 PM

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

public class Game
{
public static Random generator = new Random();
private static Scanner s = new Scanner(System.in);

public static void main(String args[])
{

int i = 0;
while(whateverYourExitConditionIs)//what is the exit condition? certain amount of runs?
{

int numberOne, numberTwo;

// Generate a random number
numberTwo = generator.nextInt(10);
System.out.println("Random number is " + numberTwo);

// Prompt the user for a number
numberOne = s.nextInt();
System.out.println("Your number is " + numberOne);

// Check guess against random number
checkAgainstRandomNumber(numberOne, numberTwo);
}
}

private static void checkAgainstRandomNumber(int num1, int num2)
{

// Compare the numbers
if (num1 == num2) {
System.out.println("You have won");
}
else if (num1 > num2) {
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
}
}
}

See code comment and then you should be set. You had an extra '{' in there earlier.

```

This post has been edited by KYA: 01 December 2008 - 05:13 PM