Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: new program (lotto.class)

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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");
    		}
    	}
    }
    Last edited by helloworld922; November 3rd, 2009 at 04:12 PM.


  2. #2
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default 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

    REVISED 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");
     
    }
    }
    Last edited by rsala004; November 3rd, 2009 at 04:21 PM.

  3. The Following User Says Thank You to rsala004 For This Useful Post:

    james137 (November 3rd, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: new program (lotto.class)

    thanks. is the program any good?

  5. #4
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: new program (lotto.class)

    tyvm on that

  6. #5
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 6 Times in 5 Posts

    Default 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.
    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
    Last edited by rsala004; November 3rd, 2009 at 04:24 PM.

  7. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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:
    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) + "): ");

  8. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: new program (lotto.class)

    yeah im on that learning curve lol thanks a lot i'll post the final one soon

  9. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default

    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 ();
     
    		do{
    		System.out.print("Pick a maximum number (larger numbers take longer): ");
    		System.out.println ("(must be greater than 2)");
    		TotalNumbers = myScanner.nextInt();
    		}while(TotalNumbers < 2);
     
    		givennumber = (myRandom.nextInt(TotalNumbers) + 1);
     
    		System.out.println("Pick a number between 1 and " + Integer.toString(TotalNumbers) + " (for example " + Integer.toString(givennumber) + "): ");
    		System.out.println("Bad responses will be forced to reenter:");
     
    		do{
    		UserNumber = myScanner.nextInt();
    		if (UserNumber > TotalNumbers) {
    			System.out.println ("Must be less than " + TotalNumbers + " Please type again:");
    		}
    		else if (UserNumber < 1){
    			System.out.println ("must be greater than 0 Please type again:" );
    		}
    		}while(UserNumber < 1 || UserNumber > TotalNumbers);
    		System.out.println ();
    		System.out.println ();
     
    			do{
        		RandomNumber = (myRandom.nextInt(TotalNumbers) + 1);
       			System.out.println (RandomNumber);
        		times += 1;
    			}while(RandomNumber != UserNumber);
     
    			 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");
    		}
    	}
    //}

    hopfully it has no bugs and it is alright
    Last edited by helloworld922; November 3rd, 2009 at 09:48 PM.