...

View Full Version : Help Closing While Loop



Lilo17x
12-08-2010, 04:04 AM
So this is what I have so far:

import java.util.Random;

public class lotteryGame
{
public static void main(String args[]) throws Exception
{
int n=0; // counter for the amount of times played
int t=0; // total amount of winnings
int i=0; // number of matches
int a; // first guess
int b; // second guess
int c; // third guess
int d; // first number in the lottery
int e; // second number in the lottery
int f; // third number in the lottery
Random rand = new Random();

while (n<100) // play the game for 100 times
{
a=rand.nextInt(9) + 1; // assign a random number to a
b=rand.nextInt(9) + 1;
c=rand.nextInt(9) + 1;
System.out.println("Lottery Number"+a+b+c);
d=rand.nextInt(9) + 1;
e=rand.nextInt(9) + 1;
f=rand.nextInt(9) + 1;
System.out.println("Your guess"+d+e+f);
if (a==d){
i=i+1;
System.out.println(a+"is a match");
d=d+10; // if a=d then d is taken out of the range (1-9)
}
else if (a==e){
System.out.println(a+"is a match");
i=i+1;
e=e+10;
}
else if (a==f){
System.out.println(a+"is a match");
i=i+1;
f=f+10;
}
if (b==d){
System.out.println(b+"is a match");
i=i+1;
d=d+10;
}
else if (b==e){
System.out.println(b+"is a match");
i=i+1;
e=e+10;
}
else if (b==f){
System.out.println(b+"is a match");
i=i+1;
f=f+10;
}
if (c==d){
System.out.println(c+"is a match");
i=i+1;
d=d+10;
}
else if (c==e){
System.out.println(c+"is a match");
i=i+1;
e=e+10;
}
else if (c==f){
System.out.println(c+"is a match");
i=i+1;
f=f+10;
}
if (i==3){
if (a==d-10&&b==e-10&&c==f-10){
t=t+10000;
System.out.println("You win!!!");
}
else{
t=t+1000;
System.out.println("You had 3 matches! You win $1000");
}
}
if (i==2){
t=t+50;
System.out.println("You had 2 matches! You win $50");
}
if (i==1){

t=t+10;
System.out.println("You had 1 match! You win $10");
}
}

n=n+1;
i=0;
}
}

I don't know what is wrong with the program, It compiles fine but when I run it, it never stops. =/
I figure that
n=n+1
would be a counter to stop the while loop. Where do I place it so it can count everytime it runs. Or, What do I need to add in order for the program to stop running after 100 times?
Thank You. =]

Logic Ali
12-08-2010, 04:39 AM
I figure that
n=n+1
would be a counter to stop the while loop. Where do I place it so it can count everytime it runs.Inside the body of the loop otherwise it's never reached.
Also as a matter of general good practice, always perform a comparison between the title of a forum and the language you want to discuss. If the test fails, continue searching.

TinyScript
12-08-2010, 04:40 AM
that's java code which is not the same as javascript. You probably want to find a java forum to get an answer



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum