简体   繁体   中英

How do I create multiple elements inside a string using java?

First off, I am new at programming and am only in my third week of programming class so I apologize if I sound inexperienced...

I am creating a program that simulates picking a card from a deck. I want to create a string of the ranks from Ace to 10. Then, I want to create a string of the four different suits. Then I want to have the computer randomly select an element from the ranks string and an element of the suits string so that it will say "The card you picked is Jack of Clubs" (just as an example).

I am having trouble creating these string variables so that the computer can randomly select an element from each string array. Can anyone help me out?

您只需要将秩存储在一个数组中,将西服存储在另一个数组中,然后在0和最大对应索引之间选择两个随机数。

You can use a String Array to hold the cards and a Random object to select one.

Example:

   String[] cards = {"one", "two", "three", "four", "five"}; // array

   Random random = new Random(); // random object 

   int index = random.nextInt(cards.length); // assign the next random int with a maximum of the array length

   System.out.println(cards[index] + " was selected.");

Don't forget to import this import java.util.Random; .

That should help.

You just need to have a list of card values and a list of suits:

String[] values = {
    "Ace", "King", "Queen", "Jack", "Ten"
};
String[] suits = {
    "Clubs", "Spades", "Hearts", "Diamonds"
}

And then you can just create a new java.util.Random and let it 'choose' a pseudorandom value for both values and suits , between 0 and array.length - 1 :

Random r = new Random();
String suit = suits[r.nextInt(suits.length)];
String value = values[r.nextInt(values.length)];

I think you do not know about enums yet, since you just had three weeks of programming class, but they usually use enums for lists with fixed values. An enum is a class with a fixed number of instances:

public enum Suit {
    CLUBS, SPADES, HEARTS, DIAMONDS;
}

public enum Rank {
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
    JACK, QUEEN, KING, ACE;
}

A deck of playing cards is a very good example of that: it is not likely that it ever changes.

Welcome to the world of programming.

I try to give you a working solution to your problem, so if this is a homework assignment, you may want to think about it a bit more before reading the answer.

You have a known, finite set of suits and ranks. To me, this sounds like an enumeration , so let us create an enum for the suits, and for the ranks.

enum Suit {
    CLUB, SPADE, HEART, DIAMOND
}

enum Rank {
    ACE, KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN, SIX
}

Now we want to randomly combine one suit with one rank. Java provides a Random class, which allows us to acces a random integer number. Also, from our enums, we can obtain an Array of enumeration constants. So, what we want to do is create this array, and randomly select one element. The code for this could look the following:

public static void main(String[] args) {
    Random random = new Random();
    Suit[] suits = Suit.values();
    Rank[] ranks = Rank.values();
    String response =
            String.format(
                    "The computer picked %s %s",
                    suits[random.nextInt(suits.length)].toString(),
                    ranks[random.nextInt(ranks.length)].toString()
            );
    System.out.println(response);
}

Suit.values() gives us the array. In an array, you can access an element by an index, which we do with the random index random.nextInt(suits.length) . Note that this gives us an integer between 0 (inclusive) and suits.length exclusive. Arrays in Java start with index zero, so this is what we want.

String.format provides nice functionality to format a string. You should have a look at it: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.util.Locale,%20java.lang.String,%20java.lang.Object...)

If you want, you could now also define a toString method, to make the string representation of the enums look nicer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM