//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
System.out.println("Enter your 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) {
System.out.println("Your number is too high");
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
System.out.println("Your number is too low");
}
}
}
Missing loop in main functiontrying to figure out how to do the loop in the main function but can
Page 1 of 1
3 Replies - 587 Views - Last Post: 01 December 2008 - 05:13 PM
#1
Missing loop in main function
Posted 01 December 2008 - 03:29 PM
Replies To: Missing loop in main function
#2
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.
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
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.

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
System.out.println("Enter your 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) {
System.out.println("Your number is too high");
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
System.out.println("Your number is too low");
}
}
}
#4
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
System.out.println("Enter your 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) {
System.out.println("Your number is too high");
}
else {
// If it is not equal or not greater than, it can only be less than
// So you can skip the test.
System.out.println("Your number is too low");
}
}
}
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|