reset password
Author Message
KA1997
Posts: 31
Posted 14:34 Feb 17, 2017 |

Does anyone know how you generate random cards using switch? I have no idea how to do that.

Arnav98
Posts: 48
Posted 14:37 Feb 17, 2017 |

Look at the lab 02 last part of the question again. Its the same logic.

KA1997
Posts: 31
Posted 14:41 Feb 17, 2017 |

But we used if else statements for that. Do I just do something similar, but with switch statements?

Arnav98
Posts: 48
Posted 14:54 Feb 17, 2017 |

Yea. Its the exactly same thing. Just use switches instead of if/else statements.

KA1997
Posts: 31
Posted 17:18 Feb 17, 2017 |

Alright. Also, do we need a switch for each suit or does 1 switch cover the entire thing?

kknaur
Posts: 540
Posted 17:53 Feb 17, 2017 |

Take a look at the ChineseZodiac problem.  This shows how to use a switch with multiple cases.  Also just play around with writing some switches and see how they work.  You should get a general idea of how they function before you try to solve the homework with them.

jgarc629
Posts: 76
Posted 02:01 Feb 18, 2017 |

If you were to fit everything in one switch, you would probably have to write all 52 conditions out, and that could be REALLY painful. Remember that you can create more than one switch. They are identical to if-else statements so just check your Lab02 and the examples on GitHub for Lecture04, it's easier than you might think!

KA1997
Posts: 31
Posted 16:10 Feb 18, 2017 |
jgarc629 wrote:

If you were to fit everything in one switch, you would probably have to write all 52 conditions out, and that could be REALLY painful. Remember that you can create more than one switch. They are identical to if-else statements so just check your Lab02 and the examples on GitHub for Lecture04, it's easier than you might think!

So should I write a switch for each suit?

sdo8
Posts: 54
Posted 18:25 Feb 18, 2017 |
KA1997 wrote:
jgarc629 wrote:

If you were to fit everything in one switch, you would probably have to write all 52 conditions out, and that could be REALLY painful. Remember that you can create more than one switch. They are identical to if-else statements so just check your Lab02 and the examples on GitHub for Lecture04, it's easier than you might think!

So should I write a switch for each suit?

Just do the same thing you did with the card numbers. I guess you can make a switch for each suit but that would probably be much more work than necessary. I'd say just go over switches again in the lecture and look at the examples/youtube it. Once you understand switches 100%, you should be able to tie one and one together. 

jgarc629
Posts: 76
Posted 01:34 Feb 19, 2017 |
KA1997 wrote:
jgarc629 wrote:

If you were to fit everything in one switch, you would probably have to write all 52 conditions out, and that could be REALLY painful. Remember that you can create more than one switch. They are identical to if-else statements so just check your Lab02 and the examples on GitHub for Lecture04, it's easier than you might think!

So should I write a switch for each suit?

Think about it this way:

Let's say I would want to generate a random result of rolling a dice and flip a coin. I would need more than one switch since there are different events happening. Each event should have its own switch. The first switch would be dedicated to the number I role on a die. The second would be dedicated to whether I landed heads or tails on the coin. The output would then be something like "You rolled a 5 and landed Heads" once I create cases within the switches that contain the possible outcomes that can happen within each individual switch. Same situation with this HW problem.

 

-Hope this helped!

Last edited by jgarc629 at 01:40 Feb 19, 2017.
KA1997
Posts: 31
Posted 20:17 Feb 19, 2017 |

I got it now! I create a switch for rank and suit since they are each "separate events". It like how rolling the dice and flipping the coin are each "separate events". Since rank and suit each have their own type of values. Then an output such as "You picked the 2 of Diamonds" comes up.