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.

new program (lotto.class)

basicly i want to know how you think of it. and see if you can see any bug or problem. I just started programing in java from nothing 2 days ago and this is what i have now: (just copy and pastle and save as lotto.java and type in cmd javac lotto.java and run by java lotto

import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
public class lotto {
public static void main (String[] args) {
Scanner myScanner = new Scanner(System.in);
Random myRandom = new Random();
int RandomNumber = 0, UserNumber = 0, times = 1, done = 0, givennumber=0, TotalNumbers = 0;
System.out.println ("As we know if you could only pick a number");
System.out.println (" out of 1 million your chances are 1 in 1 million:");
System.out.println ("today I am going to prove this...I hope");
System.out.println ();
System.out.println ("first pick how many numbers eg: 1000 or even 1000000+ (please don't add letters or sysbol)");
System.out.println ("the more this number is the longer it will take ");
TotalNumbers = myScanner.nextInt();
givennumber = (myRandom.nextInt(TotalNumbers) + 1);
System.out.println ("you can either pick a number or use a random one given");
System.out.println ("your random number is " + (givennumber) );
System.out.println ("either type this one in or type a different one(please start this on CMD.exe not some other program");
System.out.println ("as it may slow down computer if done so");
UserNumber = myScanner.nextInt();
System.out.println ();
System.out.println ();
if (TotalNumbers < 2) {
System.out.println ("error: Total number can not be less than 2");
done = done +1;
} else if (UserNumber > TotalNumbers){
System.out.println ("error: your number can't be a larger number that total number or less than 1 (almost forgot to add this in)");
done = done +1;
} else if (UserNumber < 1){
System.out.println ("You can't pick a number less than 0. Sorry");
}
else {
while (done == 0) {
RandomNumber = (myRandom.nextInt(TotalNumbers) + 1);
System.out.println (RandomNumber);
if (RandomNumber == UserNumber) {
done = done +1;
} else {
times= times + 1;
}
}
System.out.println ();
System.out.print ("it took ");
System.out.print (times);
System.out.println (" times untill your number came up, with: " +(TotalNumbers) + " likly numbers that could pop up (this is not fake)");
System.out.println ("can you belive it...want to try again");
System.out.println ("...for me it took 1765826 times picking total number = 1 million");
}
}
}

Re: new program (lotto.class)

if (TotalNumbers < 2) {
System.out.println ("error: Total number can not be less than 2");
[B]done = done +1;[/B]
} else if (UserNumber > TotalNumbers){
System.out.println ("error: your number can't be a larger number that total number or less than 1 (almost forgot to add this in)");
[B]done = done +1;[/B]
} else if (UserNumber < 1){
System.out.println ("You can't pick a number less than 0. Sorry");
}

bold lines are useless since once this if statement exits it just goes to the end of the program, never using done again.

Have you learned Do-while loops yet? (same as while loop EXCEPT it runs always AT LEAST once)
you can ask the user to enter the number and if its not good a number, you can ask again till it is correct... for example

sleeker code, idea was to get rid of the whole 'done' thing?
we used done before to tell us exactly when our 'goal' was reached...but really you ask it every time in a FOR{}, why not just ask it in the WHILE() ?

Re: new program (lotto.class)

For 2 days of learning that's not too shabby. One thing to point out is naming conventions. Not everyone follows them and it doesn't effect the performance or anything of that nature, but sun does spell out them in detail to help make code easier to understandhttp://java.sun.com/docs/codeconv/ht...ions.doc8.html
Purely convension.
You could have also used a break; statement to exit the while loop: