e. john e. john - 2 months ago 5
Java Question

trouble with if else statement... important

sorry my tittle is a little broad. but im taking computers in grade 12 and my assignment is to make a little program that generates numbers and if it is equal to another set of numbers, you win, kind of like a lottery. i found it difficult to make the random number generator work twice so i pre loaded one of the number variables. now my last task is to get it to say "you win" if one of the numbers match, and if not "you loose", basically i just cant match the variables?`

Random rn = new Random();

String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;

delay(2000);
System.out.println(answer1);

}
System.out.println("And the winning numbers are...");

delay(2000);
System.out.println(answer); }


`


ive gotten a little bit further but my 'win' wont seem to be resolved to a variable, here is my new code`

Random rn = new Random();

String answer = " 3\n 12 \n 27";
for(int i =0; i < 3; i++)
{
int answer1 = rn.nextInt(30) + 1;


System.out.println(answer1);


System.out.println("And the winning numbers are...");


System.out.println(answer);

if (answer1 == 3)
if (answer1 == 12)
if (answer1 == 27)
win = true;

if (win) {
System.out.println("You win!");
} else
System.out.println("You loose");
}`

Answer

$ Hello John. Try the code i have written Below. hope this solves your problem $pretty much. Any queries the you can contact me at flyboyravi@gmail.com. $ But this code problem is that you will win a successful lottery after long time try to make some algo for it to increase the probability for winning. Cheers Happy coading!!

import java.io.*; import java.util.Random;

public class Lottery{

    public static void main(String args[])throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    Random rn = new Random();

    int ch=1;
while(ch==1){
    String str = "";
    String str1 = "";
    String str2 = "";

    for(int i =0; i < 3; i++){
        int answer1 = rn.nextInt(9) + 1;

        //delay(2000);
        str=str+"-"+answer1;
        str1=str1+answer1;
    }
    System.out.println(str);
    str="";
    for(int i =0; i < 3; i++){
        int answer1 = rn.nextInt(9) + 1;

        //delay(2000);
        str = str+"-"+answer1;
        str2 = str2+answer1;
    }
    System.out.println(str);
    if(Integer.parseInt(str1)==Integer.parseInt(str2)){
        System.out.println("Crongrats !! You are the Lucky Winner !!!!");
    }   

    else
        System.out.println("Better Luck Next Time");

    System.out.println("Wanna Try Again ? 1=YES, 0=NO");
    ch=Integer.parseInt(br.readLine());
    ;
}

 System.out.println("Thanks for Playing Lottery with us !! Come back soon");
}

}

Comments