Missing loop in main function

trying to figure out how to do the loop in the main function but can&#

Page 1 of 1

3 Replies - 679 Views - Last Post: 01 December 2008 - 05:13 PM Rate Topic: -----

#1 grayzer  Icon User is offline

  • New D.I.C Head

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

Missing loop in main function

Post icon  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  
		 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");  
		 }  
	}  
}




Is This A Good Question/Topic? 0
  • +

Replies To: Missing loop in main function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • 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.

:rolleyes:
Was This Post Helpful? 0
  • +
  • -

#3 grayzer  Icon User is offline

  • New D.I.C Head

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

Re: Missing loop in main function

Posted 01 December 2008 - 03:56 PM

View PostMartyr2, 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.

:rolleyes:


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");  
		 }  
	}  
}


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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  
         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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1