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
Code :
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)
Code :
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
REVISED CODE:
Code :
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)");
[B]
do{
System.out.println ("Enter how many numbers, must be greater than 2:");
TotalNumbers = myScanner.nextInt();
}while(TotalNumbers < 2);[/B]
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 ();
[B]
do{
System.out.println("Enter a number, not less than 2 and not greater than "+ TotalNumbers);
System.out.println("Bad responses will be forced to reenter:");
}while(UserNumber < 2 || UserNumber > TotalNumbers);
[/B]
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)
thanks. is the program any good?
Re: new program (lotto.class)
Re: new program (lotto.class)
another good revision, for cleaner code? (btw i also used to program like that...using ints as 'on/off' toggles to signify things, but i try to avoid it.
Here we will redo the last while loop.
Code :
do{
RandomNumber = (myRandom.nextInt(TotalNumbers) + 1);
System.out.println (RandomNumber);
times += 1;
}while(RandomNumber != UserNumber);
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() ? :)
good luck
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 understand
http://java.sun.com/docs/codeconv/ht...ions.doc8.html
Purely convension.
You could have also used a break; statement to exit the while loop:
Code :
while (done == 0) {
RandomNumber = (myRandom.nextInt(TotalNumbers) + 1);
System.out.println (RandomNumber);
if (RandomNumber == UserNumber) {
break;
} else {
times= times + 1;
}
}
Something else you can add is a prompt rather than blank line letting the user know when to enter a number. eg:
System.out.print("Pick a maximum number (larger numbers take longer): ");
and
System.out.print("Pick a number between 1 and " + Integer.toString(TotalNumbers) + " (for example " + Integer.toString(givennumber) + "): ");
Re: new program (lotto.class)
yeah im on that learning curve lol thanks a lot i'll post the final one soon