即使声明方法也无法识别

时间:2018-10-18 05:42:48

标签: java

因此,我正在创建一个基本的“拱廊”项目。我有传递到游戏(方法)中的卡。我在Card类中创建了一个方法,可以选择要用于游戏的卡。

public Card choosecard(Card firstCard, Card secondCard) {

   Scanner input = new Scanner(System.in);

   Card chosen = new Card();
   boolean check = false;

   while(check == false) {

   System.out.println("What card would you like to use?\n  1. " + firstCard.name + "\n  2. " + secondCard.name);
   int answer = Integer.parseInt(input.nextLine());

   if(answer == 1) {
       chosen = firstCard;
       check = true;
   }
   else if(answer == 2) {
       chosen = secondCard;
       check = true;
   }
   else {

   }

   } //while

   return chosen;

创建此方法后,我尝试在此上下文中调用它:

public static void terminal(Card card1, Card card2) {

    boolean loop = true;
    while(loop == true) {

    System.out.println("What would you like to do?\n1. Guess Game\n2. Transfer\n3. Prizes\n4. Cancel");
    Scanner take = new Scanner(System.in);
    int answer = Integer.parseInt(take.nextLine());

    switch(answer) {

        case 1: 
            Game.GuessGame(Card.chooseCard(card1, card2));

    }

    } 

我认为可能是因为我试图在另一个方法的参数中调用一个方法。因此,我尝试从其他类中调用它,但是它仍然会给出无法找到符号的错误。

谢谢。

2 个答案:

答案 0 :(得分:0)

public Card choosecard(Card firstCard, Card secondCard)

它不是静态方法。

Game.GuessGame(Card.chooseCard(card1, card2));

但是Card.chooseCard(card1, card2)表示您是在静态调用它。

考虑将其设为静态或通过对象调用。

答案 1 :(得分:0)

Card.chooseCard(card1, card2)表示调用静态方法。您可能需要将chooseCard声明为

public static Card choosecard(Card firstCard, Card secondCard)