Java对象数组无法转换为

时间:2015-12-08 03:23:38

标签: java arrays

周围搜索但无法找到解决此问题的任何内容。

我们正在努力创造一个记忆游戏"使用卡作为对象并将卡阵列打印到控制台。当尝试使用初始值创建卡片阵列时,我们会收到错误" MemoryGame.java:25: error: incompatible types: int cannot be converted to Card"。

MemoryGame:

import java.util.Random;

public class MemoryGame{
final int BOARD_SIZE = 4;
final int NUM_SWAPS = 1000;
private Card[][] cards;  
Random rand = new Random();

public void memoryGameBoard() {
    cards = new Card[BOARD_SIZE][BOARD_SIZE];
    initializeCards();
}

private void initializeCards() {
    int row, col;
  int cardvalue = 1;
    for (row = 0; row < (BOARD_SIZE / 2); row++) {
        for (col = 0; col < BOARD_SIZE; col++){
            cards[row][col] = cardvalue;
            cardvalue++;
        }
    }
    for (row = 2; row <= (BOARD_SIZE - 1); row++) {
        for (col = 0; col < BOARD_SIZE; col++){
            cards[row][col] = cardvalue;
            cardvalue++;
        }
    }
}

/*private void initializeCards() {
    Card[] cards = new Card[numcards];
    for (int i = 0; i <= numcards - 1; i++) {
        int a = 0;
        cards[i] = a;
        a++;
        if (a == 9) {
            a = 0;
        }
    }
}*/

public void shuffleCards() {
    int row1, col1, row2, col2;
    Card temp;
    for (int i = 0; i < NUM_SWAPS; i++){
        row1 = rand.nextInt(BOARD_SIZE);
        col1 = rand.nextInt(BOARD_SIZE);
        row2 = rand.nextInt(BOARD_SIZE);
        col2 = rand.nextInt(BOARD_SIZE);

        temp = cards[row1][col1];
        cards[row1][col1] = cards[row2][col2];
        cards[row2][col2] = temp;
    }
}

public void showBoard() {
    hideBoard();
    System.out.println(this) = System.out.println(this.toString());
}

public boolean cardsMatch(int row1, int col1, int row2, int col2) {
    return cards[row1][col1].getValue() == cards[row2][col2].getValue();
}

public boolean isFaceUp(int row, int col) {
    return cards[row][col].isFaceUp();
}

public void flipCards(int row1, int col1,int row2, int col2) {
    cards[row1][col1].flipCard();
}

public String toString() {
    int col, row;
    String s = " ";
    s = s + "  1 2 3 4\n" + "=======\n";
    for (row = 1; row <= BOARD_SIZE; row++) {
        s = s + (row + "|");
        for (col = 1; col <= BOARD_SIZE; col++) {
            s = s + (cards[row][col] + " ");
        }
        s = s + "\n";
    }
}

private void hideBoard() {
    for (int i = 0; i <= 10; i++) {
        System.out.println();
    }
}

private void cardsFaceDown() {

}
}

和卡:

public class Card {
 private int value;
 private boolean faceup = false;

public void card(int v) {
    value = v;
    faceup = false;
}

public boolean isFaceUp() {
    return faceup;
}

public int getValue() {
    return value;
}

public void flipCard () {
    if (faceup) {
        faceup = false;
    } else
        faceup = true;
}
}

当尝试为数组中的卡设置值时,标记的行位于initializeCards();下。我们似乎无法将其列入价值观。我们正试图在这个布局中初始化它们:

ÏÏÏ     1 2 3 4 
ÏÏÏ    ========= 
ÏÏÏ 1 | 1 2 3 4 | 
ÏÏÏ 2 | 5 6 7 8 | 
ÏÏÏ 3 | 1 2 3 4 | 
ÏÏÏ 4 | 5 6 7 8 | 
ÏÏÏ    ========= 

代码未完成,目前我们正在努力让我们的代码按原样编译。

感谢。

2 个答案:

答案 0 :(得分:1)

我能够找到有关构造函数的更多信息,并且通过评论的一些指导来找出它。

我使用了cards[row][col] = new Card(cardvalue);

感谢指导人员!

答案 1 :(得分:0)

错误消息表明您正在尝试将this.addEventListener('install', function(event) { event.waitUntil( caches.open('v1').then(function(cache) { console.log(cache); return cache.addAll([ '/json/0.json', '/json/1.json', '/json/3.json', '/json/4.json', '/json/5.json', ]); }) ); }); this.addEventListener('fetch', function(event) { console.log(event); var response; event.respondWith(caches.match(event.request).catch(function() { return fetch(event.request); }).then(function(r) { response = r; caches.open('v1').then(function(cache) { cache.put(event.request, response); }); return response.clone(); }).catch(function() { })); }); 值分配给期望int对象的变量。

查看第25行(和第19行),您有以下内容:

Card

cards[row][col] = cardvalue; 的类型为cardvalueint期待cards[row][col]个对象。

所以你有两个选择:

  1. Card转换为卡片。例如。在之前的问题编辑中,您有cardvalue
  2. cards[row][col] = Card.card(cardvalue);更改为private Card[][] cards;