Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: :!! Unsure how to set this up/ Still learning java loops!!!!!

  1. #1
    Banned
    Join Date
    Sep 2009
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default :!! Unsure how to set this up/ Still learning java loops!!!!!

    Hello everyone, i have a program written for a lotter that ask user to input two digits and determines if it matches two randomly generated computer digits. I need help trying to Generate the first digit and use a loop to continuously generate the second digit until it is different from the first digit????


    import java.util.*;
     
    public class Lottery 
    {
    	public static void main(String [] args) 
    	{
      //  
    	int lottery = (int)(Math.random() * 100);
     
    	//
    	Scanner input = new Scanner(System.in);
     
    	System.out.println(
    	"Enter your lottery pick (two digits): ");
    	int guess = input.nextInt();
     
    	if (guess == lottery)
    		System.out.println(
    		"Exact match: you win $10,000");
     
    	else if (guess % 10 == lottery / 10 && guess / 10 == lottery / 10)
    		System.out.println(
    		"Match all digits: you win $3,000");
    	else if ( guess % 10 == lottery / 10 || guess % 10 == lottery % 10
    			|| guess / 10 == lottery / 10 || guess / 10 == lottery % 10)
    		System.out.println(
    		"Match one digit: you win $1,000");
     
    	else
    		System.out.println("Sorry no match");
     
    		}
    	}
    Last edited by Freaky Chris; September 28th, 2009 at 04:46 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: :!! Unsure how to set this up/ Still learning java loops!!!!!

    import java.util.Random;
     
     
    public class RandomExample {
    	public static void main(String[] args) throws FileNotFoundException{
    		Random r = new Random();
     
    		System.out.println(r.nextInt(42));
    		System.out.println(r.nextInt(42));
    	}
    }
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: :!! Unsure how to set this up/ Still learning java loops!!!!!

    ... what?

    throws FileNotFoundException ?

    and that still doesn't guarantee the two numbers will be different (and if it does, that's a bad random number generator!).

    In theory, it's possible that it will never finish, but I hope you can see why that's extremely remote (each time through has a .1^n chance of failing, where n is the number of times through)
    public class RandomeExample{
         public static void main(String[] args)
         {
              int num1 = (int)10*Math.random();
              int num2 = num1;
              while (num2 == num1)
              {
                   num2 = (int)10*Math.random():
              }
         }
    }

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: :!! Unsure how to set this up/ Still learning java loops!!!!!

    lmao, i just chopped out the body of some other code, didn't pay attention to that

    And i was simply showing a different way to do random numbers

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: :!! Unsure how to set this up/ Still learning java loops!!!!!

    try this one... i hope this might help you a little bit..

    this is a simple program that looks similar to what you want..

    public class LoopSample9 {
     
        public static void main(String[] args) {
     
            int targetNumber,
                guessNumber;
     
            //boolean variable to determine if the loop should end
            boolean loopEnd = false;
     
            /**
             * GENERATES THE TARGET NUMBER
             * This one is generated by Math.floor( )
             * Because the operation Math.random( ) * 10; has the range of
             * 0.92839 - 9.83928398
             *
             * So we have to add the method Math.floor to return the
             * LARGEST whole numbers that is LESS than or EQUAL to the
             * generated random number. so the range will be still 0.923823 to 9.29839283
             *
             * Now we have to add + 1 to the operation Math.random ( ) * 10 + 1; to
             * extend the range of the
             * generated random number from 1.88738273 to 10.98293829
             * So the range will now be 1.9887238723 - 10.92832983.
             *
             * Now if we add the method Math.floor( ),
             * that is Math.floor(Math.random * 10) + 1;
             * any random decimal number that will
             * be generated by the Math.random( ) * 10 + 1;
             * (e.g. 1.382983) or (e.g. 10.29839)
             * will return the LARGEST whole number LESS than or EQUAL to
             * that generated number(i.e. 1.0) or (i.e. 10.0)
             */
     
             //to avoid the number "0" (zero)
            targetNumber = (int) (Math.floor(Math.random() * 10) + 1); 
            System.out.println("Our Target Number Is: " + targetNumber);
            System.out.println("\n");
     
            //infinite loop begins here...
            while (true) {
     
                /**
                 * GENERATES THE GUESS NUMBER
                 * This is generated by Math.ceil( )
                 * This is slightly different from the operation and methods that
                 * generates the targetNumber
                 *
                 * Here, we now use the method Math.ceil, and as you can see we dont add + 1;
                 *
                 * Heres why..
     
                 * The operation Math.random( ) * 10; only generates a random number
                 * ranging from 0.29382938759 - 9.9829832983 (we already discuss it
                 * from the previews comments)
                 *
                 * So if we add the method Math.ceil( ) in this operation, that will be
                 * Math.ceil(Math.random( ) * 10;
                 * This operation will return a value of
                 * the Smallest number Greater than or equal to
                 * the generated random number (e.g. 0.982983) or (e.g. 9.823839)
                 * that will be (1.0 to 10.0)
                 *
                 * Its because the method Math.ceil( ) returns a value which is the Smallest Number
                 * GREATER than or EQUAL to that number .
                 *
                 * In this operation, if we add + 1 to the opetaion Math.ceil(Math.random * 10) + 1;
                 * it will generate a random number ranging from 1.2398983 to 10.2872873
                 * and by using the Math.ceil method it will return the value of
                 * the SMALLEST whole number
                 * GREATER than or EQAUL to that number (e.g 10.982398)
                 * it will return a number (i.e. 11.0)
                 *
                 * So we dont have to add + 1, or we dont have to
                 * extend the range of the Math.random( ) * 10;
                 *
                 * NOTE: Because by adding or using Math.ceil( ) method it
                 * will generate a random whole number
                 * ranging from 1 - 11;
                 */
     
                //to avoid the number "0" (zero)
                guessNumber =  (int) (Math.ceil(Math.random() * 10)); 
                System.out.println("Your Guess Number Is:  " + guessNumber);
     
                /**
                 * check if the guess number is equal to the target number
                 * if guess number is equal to the target number
                 * the loop will end
                 */
     
                /**
                 * if the loopEnd ends..
                 * or the loopEnd is true
                 * then the loop will break or stop
                 */
                if (guessNumber == targetNumber) {
     
                    System.out.println("");
                    System.out.println("We Have A Match! ");
                    System.out.println("The Matching Number Is:  " + guessNumber);
     
                    loopEnd = true;
                    break;
                }
     
                /**
                 * else if the loopEnd is false
                 * then it will continue to generate the loop statement
                 */
                else {
     
                    System.out.println("Let's Try Again!");
                    loopEnd = false;
                }
            }
     
        }
     
    }

    or this one..
    public class LoopSample_10 {
     
        public static void main(String[] args) {
     
            long targetNumber,
                 guessNumber;
     
            targetNumber = 34;
            System.out.println("Target Number Is:  " + targetNumber);
     
            do {
     
               guessNumber = (int) (Math.ceil(Math.random() * 100));
               System.out.println("Guess Number Is:  " + guessNumber);
            }
            while (guessNumber != targetNumber);
     
            if (guessNumber == targetNumber) {
     
                System.out.println("\n");
                System.out.println("Match Number Is:  " + guessNumber);
            }
        }
    }

Similar Threads

  1. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM
  2. Java Learning Resource for Beginners
    By Freaky Chris in forum The Cafe
    Replies: 1
    Last Post: April 29th, 2009, 04:57 AM
  3. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM
  4. Tips or suggestion to learn java
    By tj23 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2009, 05:05 AM
  5. 15, Loves ICT, learning Java coding.
    By Sergant Mitch in forum Member Introductions
    Replies: 1
    Last Post: September 20th, 2008, 09:40 AM