I've been working on this project for a few days now, I was able to complete most of it but I've been struggling on getting the five different items out of my array. I'm able to select the same item five times though.
Here's what my code looks like:
public class CardGuessingGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[] myCards = new String[]{"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"}; // Array for cards
Random rand = new Random(); //random construction
rand.nextInt(13); // Sets rand to 0-12 int
//int randomNums = rand.nextInt(13); // sets randNums from 0-12
int randomNums = rand.nextInt(myCards.length); //Randomizes "randomNums to the length of the array"
String cardInHand = myCards[(int)randomNums];
for (randomNums=0; randomNums < 5; randomNums++) {
System.out.println(cardInHand);
}
System.out.print("Guess the card in my hand: "); // Prints to user asking for guess
Scanner answer = new Scanner(System.in); // gets user input
String s = answer.nextLine(); //stores user input inside variable s
if(s.equals(cardInHand)) {
System.out.println("I do have that card in hand");
} else {
System.out.println("I do not have that card in hand!");
}
System.out.print("These were the cards I had in hand: ");
System.out.println(cardInHand);
}
}
and here's the output
run:
four
four
four
four
four
Guess the card in my hand: four
I do have that card in hand
These were the cards I had in hand: four
What I have right now works, but not correctly.
Aucun commentaire:
Enregistrer un commentaire