Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By chipp

Thread: Just curious if my code could be more concise

  1. #1
    bohrstein is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Just curious if my code could be more concise

    Java Code:
    import java.util.*;
    public class test1 {
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		System.out.print("Would you like to draw a card? ");
    		String answer = keyboard.nextLine();
    		int random = (int)(Math.random() * 13) +1;
    		int card = (int)(Math.random() * 5) +1;
    		String card1 = Integer.toString(card);		
    		for(int i = 0; i < 1; i = i +1)
    		{
    			if(answer.equals("yes") || answer.equals("Yes"))
    			{
    				if(card == 1)	
    				{
    					if(random == 11 || random == 12 || random == 13 || random == 1)
    					{
    						if(random == 1)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 11)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Jack of " + card1);
    						}
    						else if(random == 12)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Queen of " + card1);
    						}
    						else if(random == 13)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the King of " + card1);
    						}
    						
    					}
    					if(random > 1 && random < 11)
    					{
    						card1 = "Spades";
    						System.out.println("Your card is the " + random + " of " + card1);
    					}
    				}
    				if(card == 2)
    				{
    					if(random == 11 || random == 12 || random == 13 || random == 1)
    					{
    						if(random == 1)
    						{
    							card1 = "Hearts";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 11)
    						{
    							card1 = "Hearts";
    							System.out.println("Your card is the Jack of " + card1);
    						}
    						else if(random == 12)
    						{
    							card1 = "Hearts";
    							System.out.println("Your card is the Queen of " + card1);
    						}
    						else if(random == 13)
    						{
    							card1 = "Hearts";
    							System.out.println("Your card is the King of " + card1);
    						}
    						
    					}
    					if(random > 1 && random < 11)
    					{
    						card1 = "Hearts";
    						System.out.println("Your card is the " + random + " of " + card1);
    					}
    				}
    				if(card == 3)
    				{
    					if(random == 11 || random == 12 || random == 13 || random == 1)
    					{
    						if(random == 1)
    						{
    							card1 = "Diamonds";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 11)
    						{
    							card1 = "Diamonds";
    							System.out.println("Your card is the Jack of " + card1);
    						}
    						else if(random == 12)
    						{
    							card1 = "Diamonds";
    							System.out.println("Your card is the Queen of " + card1);
    						}
    						else if(random == 13)
    						{
    							card1 = "Diamonds";
    							System.out.println("Your card is the King of " + card1);
    						}
    						
    					}
    					if(random > 1 && random < 11)
    					{
    						card1 = "Diamonds";
    						System.out.println("Your card is the " + random + " of " + card1);
    					}
    				}
    				if(card == 4)
    				{
    					if(random == 11 || random == 12 || random == 13 || random == 1)
    					{
    						if(random == 1)
    						{
    							card1 = "Clubs";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 11)
    						{
    							card1 = "Clubs";
    							System.out.println("Your card is the Jack of " + card1);
    						}
    						else if(random == 12)
    						{
    							card1 = "Clubs";
    							System.out.println("Your card is the Queen of " + card1);
    						}
    						else if(random == 13)
    						{
    							card1 = "Clubs";
    							System.out.println("Your card is the King of " + card1);
    						}
    						
    					}
    					if(random > 1 && random < 11)
    					{
    						card1 = "Clubs";
    						System.out.println("Your card is the " + random + " of " + card1);
    					}
    				}
    							
    			}
    			if(answer.equals("no") || answer.equals("No"))
    				System.out.println("LAME!!!!");
    			//if(answer != "no" && answer!= "No" && answer == "yes" && answer != "Yes")
    			//{
    				//System.out.println("That is invalid");
    				
    			//}
    				
    				
    		
    		}
    	
    	}
    
    }
    Last edited by bohrstein; 09-25-2011 at 05:35 AM. Reason: mess up

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Just curious if my code could be more concise

    Just briefly eyeballing it, I'd say yes, it can be more concise, but I can't really tell you details because I can't read your code. Please edit your post above and addcode tags above and below your posted code. Something like this:

    [code]

    your code goes here

    [/code]

  3. #3
    bohrstein is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    ok I fixed it

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Just curious if my code could be more concise

    You've got a ton of redundancies. All the code that looks the same could be made into one single block.

    Evaluate the suit in one block, and then evaluate the rank or value of the card in a second block of code. Consider use of arrays as well to simplify some of this. I'd also recommend enums, but I'm guessing that this is for school and you're not allowed to use them.

    Also, that's a heck of a lot of aces. :)
    Last edited by Fubarable; 09-25-2011 at 12:31 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Just curious if my code could be more concise

    For instance, if you put your suit Strings into an array of String, you could get the suit name in one line of code. The same can be done for the card value names.

  6. #6
    bohrstein is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    yea it is for school and we haven't learned arrays set so I was just seeing what i could do with what I knew. And how would you write it as an array or enum; could you explain enum?

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    long time not "come" to the forum :D btw, this:

    Java Code:
    					if(random == 11 || random == 12 || random == 13 || random == 1)
    					{
    						if(random == 1)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 11)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 12)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						else if(random == 13)
    						{
    							card1 = "Spades";
    							System.out.println("Your card is the Ace of " + card1);
    						}
    						
    					}
    i think you should do like this:

    Java Code:
    	if(random == 11 || random == 12 || random == 13 || random == 1) {
    		card1 = "Spades";
    		System.out.println("Your card is the Ace of " + card1);
    	}
    simplified...

  8. #8
    bohrstein is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    I am an idiot, it should have been ace, jack, queen, king

  9. #9
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    so it should be like this:
    Java Code:
    switch (random) {
    case 1: card1 = "ace"; break;
    case 11: card1 = "jack"; break;
    case 12: card1 = "queen"; break;
    case 13: card1 = "king"; break;
    default: break;
    }
    System.out.println ("Your card is the " + card1);
    bohrstein likes this.

  10. #10
    bohrstein is offline Member
    Join Date
    Sep 2011
    Posts
    6
    Rep Power
    0

    Default Re: Just curious if my code could be more concise

    oh okay, I wasn't sure if you could have a switch within an if statement, thanks

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Curious
    By Cutter in forum New To Java
    Replies: 3
    Last Post: 09-14-2009, 06:25 AM
  4. curious about iterator
    By jacline in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 04:29 AM
  5. A Curious Programming Hobbyist :)
    By Jesdisciple in forum Introductions
    Replies: 4
    Last Post: 05-11-2008, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •