:!! 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????:!!:!!

Code :

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

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

Code :

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

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)

Code :

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():
}
}
}

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 :P

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

Chris

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..

Code :

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..
Code :

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