Trying to teach myself Java, for better or for worse. The program I intend to create should pick a random playing card from a deck of cards, determine the suit and face value, then return all variables as shown: "You picked 14, the 2 of Hearts."

The problem is I can not use the strings I created during the if/else-if statements in my final print statement:

package playing_card_game;

import java.util.*; // Needed for a Random Number Generator

public class Cards {

	]
	public static void main(String[] args) {
		
		Random rand = new Random();
		int cardNumber = rand.nextInt(52);
		
		
		int suitName = cardNumber/13;
			
			if (suitName == 0){
				String s = "Spades";}
			
			else if (suitName == 1){
				String s = "Hearts";}
			
			else if (suitName == 2){
				String s = "Clubs";}		
			
			else if (suitName == 3);{
				String s = "Diamonds";}			
			
		int faceValue = suitName%13;
		
			if (faceValue == 0){
				String f = "Ace.";}
			
			else if (faceValue == 1){
				String f = "2.";;}
			
			else if (faceValue == 2){
				String f = "3.";}		
			
			else if (faceValue == 3){
				String f = "4.";}
			
			else if (faceValue == 4){
				String f = "5";}
			
			else if (faceValue == 5){
				String f = "6.";}		
			
			else if (faceValue == 6){
				String f = "7.";}
			
			else if (faceValue == 7){
				String f = "8.";}
			
			else if (faceValue == 8){
				String f = "9.";}		
			
			else if (faceValue == 9){
				String f = "10.";}
			
			else if (faceValue == 10){
				String f = "11.";}
			
			else if (faceValue == 11){
				String f = "12.";}		
			
			else if (faceValue == 12);{
				String f = "13.";}
			
		System.out.println("You picked " + cardNumber + ", the " + f + " of " + s + ".");
	}

}

Recommended Answers

All 6 Replies

Simplified: Why can I not use a string created during an if/else statement in my summarizing Print statement?? Is there a rule I am unaware of??

It's a practice to always declare variables at the start of the program
Don't initialize the strings s and f inside an if statement
initialize them from the start and remove all the declarations in the if statements

Why can I not use a string created during an if/else statement

my guess is the compiler wouldn't find the symbols used in line 69 at compile time

It all about variable scope, which I'm sure you're aware of. Do it as zeroliken said, he's right.

Thank you, the issue with the string is all better. Unfortunately, the only values for "s" and "f" that appear during the Print statement are Diamonds and 13, respectively.

So no matter which number the random generator draws, the end result always is a suit name of Diamonds and a face value of 13. Logic error?

Line 28 looks like the wrong variable is used for the % operation.

Since this is a learning exercise, here are a couple of hints about different (better) ways to write some of that code...
You use nested if statements to get a String that corresponds to a numeric value.
First hint: you can use a switch statement to do the same thing much more clearly.
Second: you could use an array of Strings to avoid all those if tests completly, eg

String[] dayNames = {"Monday", "Tuesday ....
int dayNumber = (some vaue 0 - 6)
String dayName = dayNames[dayNumber];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.