用java写一个猜牌游戏
时间: 2023-12-19 09:06:03 浏览: 147
这是一个简单的猜牌游戏,使用Java语言编写。游戏规则是:程序会随机生成一张扑克牌,玩家需要猜测这张牌的花色和点数,如果猜对了就赢得胜利,否则就失败。
代码如下:
```java
import java.util.Scanner;
public class GuessCardGame {
public static void main(String[] args) {
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int suitIndex = (int) (Math.random() * 4);
int rankIndex = (int) (Math.random() * 13);
String card = ranks[rankIndex] + " of " + suits[suitIndex];
System.out.println("I'm thinking of a card. Can you guess what it is?");
Scanner scanner = new Scanner(System.in);
String guess = scanner.nextLine();
if (guess.equalsIgnoreCase(card)) {
System.out.println("Congratulations! You guessed the card.");
} else {
System.out.println("Sorry, the card was " + card + ".");
}
}
}
```
运行程序后,会输出一句话提示玩家开始猜牌。玩家需要在控制台输入自己的猜测,程序会将猜测与随机生成的扑克牌进行比较,如果猜对了就输出胜利的信息,否则输出失败的信息。
阅读全文