Results 1 to 10 of 10
- 09-24-2011, 11:55 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
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
-
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]
- 09-25-2011, 12:03 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Just curious if my code could be more concise
ok I fixed it
-
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.
-
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.
- 09-25-2011, 01:58 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
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?
- 09-25-2011, 04:59 AM #7
Re: Just curious if my code could be more concise
long time not "come" to the forum :D btw, this:
i think you should do like 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); } }
simplified...Java Code:if(random == 11 || random == 12 || random == 13 || random == 1) { card1 = "Spades"; System.out.println("Your card is the Ace of " + card1); }
- 09-25-2011, 05:31 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Re: Just curious if my code could be more concise
I am an idiot, it should have been ace, jack, queen, king
- 09-25-2011, 06:01 AM #9
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);
- 09-25-2011, 05:30 PM #10
Member
- Join Date
- Sep 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
Curious
By Cutter in forum New To JavaReplies: 3Last Post: 09-14-2009, 06:25 AM -
curious about iterator
By jacline in forum New To JavaReplies: 1Last Post: 03-16-2009, 04:29 AM -
A Curious Programming Hobbyist :)
By Jesdisciple in forum IntroductionsReplies: 4Last Post: 05-11-2008, 10:31 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks