3 Replies - 273 Views - Last Post: 16 February 2012 - 10:18 AM Rate Topic: -----

#1 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Can you show me an improved version of my program?

Posted 16 February 2012 - 09:50 AM

Cheers if you can. I'm trying to see how I could my this program more readable. The aim is too make a slot machine which tells you, you win if you the same numbers.



import java.util.Random;

public class PP59 {

    public static void main(String[] args) {
        int Random1, Random2, Random3;
       


 


        Random generator = new Random();


        while (in.hasNextLine()) {
            
        

        Random1 = generator.nextInt(9);
        Random2 = generator.nextInt(9);
        Random3 = generator.nextInt(9);


        System.out.print(Random1);
        System.out.print(Random2);
        System.out.println(Random3);

        if (Random1 == Random2 && Random1 == Random3 || Random2 == Random1 && Random2 == Random3 || Random3 == Random1 && Random3 == Random2) {
            System.out.println("You won 50 :D/>");

        }
            if (Random1 == Random2 || Random2 == Random3 || Random1 == Random3) {
            System.out.println("You won 25 :D/>");

        }





    }
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Can you show me an improved version of my program?

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Can you show me an improved version of my program?

Posted 16 February 2012 - 10:13 AM

In the line:
 while (in.hasNextLine())


Where is declared 'in' ?.

And this is most readable.
if ( (Random1 == Random2 && Random1 == Random3 ) || (Random2 == Random1 && Random2 == Random3)
		|| (Random3 == Random1 && Random3 == Random2) ) {

         System.out.println("You won 50 :D/>");
			
 }else if ( (Random1 == Random2) || (Random2 == Random3) || (Random1 == Random3) ) {

    System.out.println("You won 25 :D/>");

 }


Was This Post Helpful? 1
  • +
  • -

#3 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Re: Can you show me an improved version of my program?

Posted 16 February 2012 - 10:18 AM

Ahhh Cheers that looks a lot better. Thanks for the tips
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Can you show me an improved version of my program?

Posted 16 February 2012 - 10:18 AM

You don't need the extra blank lines; a source file header that describes what the file does, who wrote it and when would be nice; an appropriate comment now and then; and better variable names - avoid using the same subscripted name over and over.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1