Jump to content

Someone please help me :( I can't figure out why this won't work. It suppose to pick a random card and suite.

 

import java.util.Random;

public class HW03P06{
	public static void main(String[] args){
			
			int cardRandom = (1 + (int)(Math.random() * ((13-1) + 1)));
			int suiteRandom = (1 + (int)(Math.random() * ((4-1) + 1)));
			
			String card;
			switch (cardRandom){
			case 1: card = new String("Ace");
			break;
			case 2: card = new String("2");
			break;
			case 3: card = new String("3");
			break;
			case 4: card = new String("4");
			break;
			case 5: card = new String("5");
			break;
			case 6: card = new String("6");
			break;
			case 7: card = new String("7");
			break;
			case 8: card = new String("8");
			break;
			case 9: card = new String("9");
			break;
			case 10: card = new String("10");
			break;
			case 11: card = new String("Jack");
			break;
			case 12: card = new String("Queen");
			break;
			case 13: card = new String("King");
			break;
			}
			
			String suite;
			switch (suiteRandom){
			case 1: suite = new String("Diamonds");
			break;
			case 2: suite = new String("Clubs");
			break;
			case 3: suite = new String("Hearts");
			break;
			case 4: suite = new String("Spades");
			break;
			}

		System.out.println("The card you picked is " + card + "of" + suite);
	}
}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/583595-need-help-with-beginner-java-hw/
Share on other sites

Link to post
Share on other sites

Why don't you try a line like:

 

System.out.println("card:" + cardRandom + "suite:" + suiteRandom);

 

 

Before your case statements.

 

And see if it actually spits out the random numbers like you believe it should.

 

I think I can see what the problem is, but that would make life too easy for you!
 

Link to post
Share on other sites

3 minutes ago, Mark77 said:

Why don't you try a line like:

 

System.out.println("card:" + cardRandom + "suite:" + suiteRandom);

 

 

Before your case statements.

 

And see if it actually spits out the random numbers like you believe it should.

 

I think I can see what the problem is, but that would make life too easy for you!

 

 

LOL Sorry to waste your effort but I think I solved it. I took off the "new" in my case statements. It seems to be working.
 


import java.util.Random;

public class HW03P06{
	public static void main(String[] args){
			
			int cardRandom = (1 + (int)(Math.random() * ((13-1) + 1)));
			int suiteRandom = (1 + (int)(Math.random() * ((4-1) + 1)));
			
			String card = "";
			switch (cardRandom){
			case 1: card = "Ace";
			break;
			case 2: card = "2";
			break;
			case 3: card = "3";
			break;
			case 4: card = "4";
			break;
			case 5: card = "5";
			break;
			case 6: card = "6";
			break;
			case 7: card = "7";
			break;
			case 8: card = "8";
			break;
			case 9: card = "9";
			break;
			case 10: card = "10";
			break;
			case 11: card = "Jack";
			break;
			case 12: card = "Queen";
			break;
			case 13: card = "King";
			break;
			default: card = "Error";
			break;
			}
			
			String suite = "";
			switch (suiteRandom){
			case 1: suite = "Diamonds";
			break;
			case 2: suite = "Clubs";
			break;
			case 3: suite = "Hearts";
			break;
			case 4: suite = "Spades";
			break;
			default: card = "Error";
			break;
			}

		System.out.println("The card you picked is " + card + " of " + suite);
	}
}

If anyone has a better way for me to clean it up that would be great!
 

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to post
Share on other sites

package randomCardDraw;
import java.util.concurrent.ThreadLocalRandom;

public class RandomCards{
    public static void main(String[] args){
            
        
        
            int cardRandom = ThreadLocalRandom.current().nextInt(1, 13 + 1);
            int suiteRandom = ThreadLocalRandom.current().nextInt(1, 4 + 1);

			// now your switch case :)
           

 

Might end up being slightly more efficient.

 

Not going to end up making much of a differnece though :)

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

Take a look at your card switch statement. 9 out of 14 cases simply state if the random number is that number then set the card to that number. You can save a lot of code by saying card = cardRandom and then changing the value of card if cardRandom is not between 2 and 10.

 

You would also be better off moving these cases into new functions. Then instead of:

case 1: card = "Ace";

You could use a return like:

case 1: return "Ace";

So then instead of the repeated "card = x" that you have now you could use:

card = getCardValue(cardRandom);

These changes would make the code a lot shorter and tidier.

Link to post
Share on other sites

@IvanSnipedYu what actually fixed your code was the initialization of suite and card to the null string. Compiler errors are a godsend ;)

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×