gpt4 book ai didi

java - 猜谜游戏java类的问题~

转载 作者:行者123 更新时间:2023-12-01 19:05:17 25 4
gpt4 key购买 nike

这是我制作的猜谜游戏的代码,但问题是,有几个问题是我作为java初学者不擅长的,需要一些指导。沿着代码,我用侧面的箭头突出显示了一些错误。

    import java.util.*;

public class GuessingGame
{


private static Player house;
private static Player player;

private static int wins;
private static int loses;
private String name;
int card1,card2;
private int value;



public void Player(String name){

this.name=name;
card1 = (Integer) null;
card2 = (Integer) null;
}



public void Card(int value){

this.value = value;
}





public int getValue(){
return value;
}



public void acceptDeal(Card card1, Card card2){
Random r = new Random();
int value = r.nextInt(13) + 1;
card1 = new Card(value); <<<<<<<<======= Error 1
value = r.nextInt(13) + 1;
card2 = new Card(value); <<<<<<<<======= Error 2
}



public static void init()
{

house = new Player("House"); <<<<<<<<======= Error 3
player = new Player("Player"); <<<<<<<<======= Error 4
wins = 0;
loses = 0;

}


public static void playGame()
{
Scanner scan = new Scanner(System.in);

char option, playAgain;
int houseHandStrength, playerHandStrength;
System.out.println("Welcome to our card guess 1.0 game!");
System.out.println();

do {
// Deal cards to the house and player.
house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength)); <<<<<=== Error 5
player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength)); <<<<<=== Error 6

System.out.println(house);

// Determine whether the player wants to play this hand.
do {
System.out.print("Deal cards? (Y/N) ");
option = Character.toLowerCase(scan.next().charAt(0));
}
while (option != 'n' && option != 'y');

if (option == 'y')
{
System.out.println(player);

// Display hand strength of both players.
houseHandStrength = house.getHandStrength(); <<<<<=== Error 7
playerHandStrength = player.getHandStrength(); <<<<<=== Error 8
System.out.println("The dealer's hand strength is: " + houseHandStrength);
System.out.println("Your hand strength is: " + playerHandStrength);
System.out.println();

// If the player has a stronger hand.
if (player.getHandStrength() > house.getHandStrength())
{
System.out.println("** You won the hand! **");
wins++;
}
else {
System.out.println("The house wins this round!");
loses++;
}
}

// Display the win/lose statistics.
System.out.println("Current wins: " + wins);
System.out.println("Current loses: " + loses);

// Prompt whether the user wants to play again.
do {
System.out.print("Would you like to play again? (Y/N) ");
playAgain = Character.toLowerCase(scan.next().charAt(0));
}
while (playAgain != 'n' && playAgain != 'y');

System.out.println();
System.out.println("*******************************************************");
}
while (playAgain == 'y');

System.out.println();
System.out.println("Thank you for playing!");
}

public static void main(String[] args)
{
init();
playGame();
}
}

最佳答案

首先欢迎来到 StackOverflow。很高兴看到您找到并使用了作业标签。请记住,为了让人们能够帮助您,您需要提供更多信息。错误是什么意思,运行代码时会发生什么等

关于您收到的错误,看来您还没有真正定义类 CardPlayer,您的代码中有两个方法 GuessingGame 类中的 GuessingGame.Card()GuessingGame.Player() 。将它们更改为内部(或外部)类,应该没问题;)

关于java - 猜谜游戏java类的问题~,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10332811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com