我已经在这个项目上工作了几天了,我已经完成了大部分工作,但我一直在努力从我的数组中提取五个不同的项。不过,我可以选择相同的项目五次。
下面是我的代码:
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);
}
}
这是输出
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
我现在所拥有的是有效的,但不正确。
发布于 2018-09-13 07:15:16
我写了新的代码,以提高您在阅读源代码时的歧义。所以我在一些行中添加了注释,以便更清楚地理解。
public class CardGuessingGame {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//list of cards
List<String> myCards = Arrays.asList("two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace");
//shuffle cards
Collections.shuffle(myCards);
//get first 5 elements from shuffled list
HashSet<String> myHand = new HashSet<>();
for (int i = 0; i < 5; i++) {
myHand.add(myCards.get(i));
System.out.println(myCards.get(i));
}
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 set contains , you found the card
if (myHand.contains(s)) {
System.out.println("I have that card in my hand");
} else {
System.out.println("I do not have that card in hand!");
}
System.out.print("These are the cards which is in my hand: ");
//write all cards in my hand with sepearete comma
System.out.println(String.join(",", myHand));
}
}
发布于 2018-09-13 07:18:34
这是一个有效的代码,并且只接受唯一的卡片。首先,你应该弄清楚哪些变量是数组,哪些不是。
对rand.nextInt(13);
的调用不会将Random实例设置为生成1到13之间的随机数,但实际上会生成一个随机数。
你的随机数生成应该放在循环中。手头的卡片需要包含多个值,因此应该使用数组。
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
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
String[] cardsInHand = new String[5];
List<String> cards = new LinkedList<>(Arrays.asList(myCards));
Collections.shuffle(cards);
for (int i = 0; i < 5; i++) {
//This will not give you unique cards!
//int randomNums = rand.nextInt(myCards.length); // Randomizes "randomNums to the length of the array"
cardsInHand[i] = cards.remove(0);
}
System.out.println(Arrays.toString(cardsInHand));
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
//Some kind of contains method. You can traverse the array use a list or do however you like
Arrays.sort(cardsInHand);
if (Arrays.binarySearch(cardsInHand,s) >= 0) {
System.out.println("I do have that card in hand");
} else {
System.out.println("I do not have that card in hand!");
}
//Close the stream
answer.close();
System.out.print("These were the cards I had in hand: ");
System.out.println(Arrays.toString(cardsInHand));
}
}
https://stackoverflow.com/questions/52304357
复制相似问题