Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,511 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,917 people online right now. Registration is fast and FREE... Join Now!




Missing loop in main function

 

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

grayzer

1 Dec, 2008 - 02:29 PM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 19


My Contributions
CODE


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



User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Missing Loop In Main Function

1 Dec, 2008 - 02:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,246



Thanked: 820 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.gif
User is offlineProfile CardPM
+Quote Post

grayzer

RE: Missing Loop In Main Function

1 Dec, 2008 - 02:56 PM
Post #3

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 19


My Contributions
QUOTE(Martyr2 @ 1 Dec, 2008 - 02: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.gif


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.

CODE

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

User is offlineProfile CardPM
+Quote Post

KYA

RE: Missing Loop In Main Function

1 Dec, 2008 - 04:13 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 11,192



Thanked: 489 times
Dream Kudos: 2825
Expert In: C, C++, Java

My Contributions
java


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: 1 Dec, 2008 - 04:13 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:13AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month