문제

I am trying to create a program that creates a deck of 52 cards. When I try to compile the code, it keeps saying "cannot find symbol on all of the getName getters in the createDeck method. I'm new to Java programming so I'm at a loss as to what to do. Any suggestions would be helpful. Here's the code:

public class Card {


private int value;
 private String name;
 private String suit;

 public void setValue(int v){
  value = v;
 }
 public int getValue(){
  return value;
 }

 public void setName(String n){
  name = n;
 }
 public String getName(){
  return name;
 }

 public void setSuit(String s){
  suit = s;
 }
 public String getSuit(){
  return suit;
 }

}

This class should create a deck:

public class cardDeck {
 int[] values = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
 String[] names = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "King", "Queen", "Jack", "Ace",};
 String[] suits = {"Clubs", "Spades", "Hearts", "Diamonds"};

 public Card[] createDeck (Card[] d){
 for(int j:deck){
  d[j] = new Card();
  for(String k:names){
   d[j].setName(k);
 }
  for(int i:values){
   if((d[j].getName == ("King")) || (d[j].getName == ("Queen")) || (d[j].getName == ("Jack"))){
      d[j].setValue(10);
    }else if(d[j].getName == ("Ace")){
      d[j].setValue(11);
    }else{
  d[j].setValue(i);
    }
  }
  for(String l:suits){
   d[j].setSuit(l);
  }
 }
 return d;
} 
}

Here's the tester class:

 class CardTest {
 public static void main(String[] args){
  Card[] deck = new Card[52];
  cardDeck playingCards = new cardDeck();
  playingCards.createDeck(deck);

  for(int q = 0; q < 52; q++){
   System.out.println(deck[q]);
  }
 }
} 
도움이 되었습니까?

해결책

You are missing parentheses for the method call:

if((d[j].getName() == ("King")) ...

Some languages (such as ActionScript) support adding getters that look like properties, instead of methods. In Java, there is no such special syntax for getters and setters, they are simply normal methods, and all methods must be accessed only with standard method call syntax — () — even if it has no parameters.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top