【问题标题】:I have to make a "guess next card is higher" game. but can't find the problem我必须做一个“猜下一张牌更高”的游戏。但找不到问题
【发布时间】:2019-10-31 15:54:16
【问题描述】:

3 类。卡片、游戏和甲板。 我不知道为什么它给我一个错误。 希望有人可以帮助我,我想知道问题是什么。

标题是什么,我必须猜测我会得到的下一张牌是更高还是更低。


package CardGame;

import java.util.Scanner;
import java.util.*;

public class Game {


    private static int score;

    // currentCard : Card
    private static Card currentCard;
    // Next Card
    private Card nextCard;
    // Scanner
    private static Deck deck;

    private static Scanner sc = new Scanner(System.in);

    // Main

    public static void main(String[] args) {

        deck = new Deck();

        currentCard = deck.getNextCard();

        Game.gameTurn();
    }

    // get turn method
    public static void gameTurn() {
        // System.out.println(Deck.getCards().size());
        System.out.print("is your next card higher or lower than the next card?");
        System.out.print(sc);
        String answer = sc.nextLine();

        Card nextCard = deck.getNextCard();


        if (answer.equals("higher") && nextCard.isHigherOrEqual(currentCard)) {

            correct();
        } else if (answer.equals("lower") && !nextCard.isHigherOrEqual(currentCard)) {
            correct();
            // answer.equals("lower")&& !nextCard.isHigherOrEqual(currentCard)
        } else {
            gameOver();
        }
    }

    // correct method

    public static void correct() {
        Game.gameTurn();
        score = 0;
        score++;
    }

    // Game over Method

    public static void gameOver() {
        System.out.println("total score is " + score + "!");
    }
}

package CardGame;

import java.util.ArrayList;
import java.util.Collections;

public class Deck {

    // cards = Card = new ArrayList<>()
    private static ArrayList<Card> cards = new ArrayList<>();

    // Deck Method
    public void Deck() {

        for (int i = 1; i < 4; i++) {
            String suits;

            switch (i) {

            case 1:
                suits = "Hearts";
                break;

            case 2:
                suits = "Diamonds";
                break;

            case 3:
                suits = "Spades";
                break;

            case 4:
                suits = "Clubs";
                break;
            default:
                suits = "";
                break;
            }
            for (int j = 1; j <= 10; j++) {
                int value = j;
                String name = j + " of " + suits;
                Card c = new Card(suits, name, value);

                cards.add(c);

            }
            // add 1.3 tot 1.11

            Card jack = new Card(suits, "jack of " + suits, 11);

            cards.add(jack); // JACK

            Card queen = new Card(suits, "queen of " + suits, 12);

            cards.add(queen); // QUEEN

            Card king = new Card(suits, "king of " + suits, 13);

            cards.add(king); // KING

            Card ace = new Card(suits, "ace of " + suits, 14);

        }
        // 1.11 shuffle cards
        Collections.shuffle(cards);
    }

    // getNextCard() : Card

    public Card getNextCard() {

        Card nextCard = cards.remove(0);

        return nextCard;

    }

    // getCards() : ArrayList <card>
    public static ArrayList<Card> getCards() {

        return cards;
    }

}

package CardGame;

public class Card {

    // Suit String
    private String suit;

    // Name String
    private String name;
    // Value int
    private int value;

    // Card constructor
    public Card(String suit, String name, int value) {
        this.value = value;
        this.name = name;
        this.suit = suit;

    }

    public int getValue() {
        return value;
    }

    // toString Method
    public String toString() {
        if (value == 15);
        return "";
    }
    // isHigherorEqual Method

    public boolean isHigherOrEqual(Card c) {
        if (this.value >= c.getValue()) {
            return true;
        } else {
            return false;
        }
    }
}

【问题讨论】:

    标签: java class if-statement arraylist game-physics


    【解决方案1】:

    中删除空白

    公共无效甲板()

    当你创建一个对象时,你调用的是一个构造函数(即使你没有写一个),而不是一个方法。 https://beginnersbook.com/2013/03/constructors-in-java/

    【讨论】:

    • 谢谢!它运行。遗憾的是找不到我无法打印 currentCard 的原因(做了一些小改动)
    • 在你的 Card 类中添加一个 getter 类 String getName() { return name;并在打印方法中调用它 System.out.println(currentCard.getName());
    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多