[英]Eclipse Java Compiler Errors
以下錯誤消息是什么意思?
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token "void", @ expected
Syntax error on token "]", :: expected after this token
Syntax error, insert "enum Identifier" to complete EnumHeader
它不喜歡的行是:
public static void main(String[] args) {
如果您需要查看上下文,我的完整 WIP 代碼如下。 Eclipse 會自動設置在該行中,我以前從未遇到過問題。
public class Card {
public class cardValue()
public static int suit;
public static int faceValue;
{
static int getfaceValue()
{
return faceValue;
}
setfaceValue(String faceValue)
{
cardFaceValue = faceValue;
return faceValue;
}
static int getSuit()
{
return suit;
}
setSuit(int suit)
{
cardSuit = suit;
return suit;
}
}
public static void main(String[] args) {
cardValue card1 = new cardValue();
// Suit values into strings for Hearts,Spades,Clubs, Diamonds
if (cardValue.getSuit() == 1)
{
System.out.print(" of Hearts");
}
if (cardValue.getSuit() == 2)
{
System.out.print(" of Spades");
}
if (cardValue.getSuit() == 3)
{
System.out.print(" of Clubs");
}
if (cardValue.getSuit() == 4)
{
System.out.print(" of Diamonds");
}
System.out.println(card1.getSuit());
}
}
我真的建議搜索並學習 Java 中的類和對象。
但如果我明白你想要做的事情,這就會奏效:
public class Card {
private int suit;
private int faceValue;
public Card (int suit, int faceValue) {
setSuit(suit);
setFaceValue(faceValue);
}
int getFaceValue () {
return faceValue;
}
void setFaceValue (int faceValue) {
this.faceValue = faceValue;
}
int getSuit () {
return suit;
}
void setSuit (int suit) {
this.suit = suit;
}
public static void main (String[] args) {
Card card = new Card(1, 4);
System.out.print(card.getFaceValue());
// Suit values into strings for Hearts,Spades,Clubs, Diamonds
if (card.getSuit() == 1) {
System.out.println(" of Hearts");
} else if (card.getSuit() == 2) {
System.out.println(" of Spades");
} else if (card.getSuit() == 3) {
System.out.println(" of Clubs");
} else if (card.getSuit() == 4) {
System.out.println(" of Diamonds");
}
}
}
如果您在類聲明中有導入,則需要在類聲明之前。
import java.io.*;
import java.util.*;
或類似
檢查您的縮進,這是不正確的。 您的 {} 可能失去平衡。
class Card {
private int suit;
private int faceValue;
public Card (int suit, int faceValue)
{
setSuit(suit);
setfaceValue(faceValue);
}
int getfaceValue () {
return faceValue;
}
void setfaceValue (int faceValue) {
this.faceValue = faceValue;
}
int getSuit () {
return suit;
}
void setSuit (int suit) {
this.suit = suit;
}
public static void main (String[] args) {
Card card = new Card(1, 4);
System.out.print(card.getfaceValue());
// Suit values into strings for Hearts,Spades,Clubs, Diamonds
if (card.getSuit() == 1) {
System.out.println(" of Hearts");
} else if (card.getSuit() == 2) {
System.out.println(" of Spades");
} else if (card.getSuit() == 3) {
System.out.println(" of Clubs");
} else if (card.getSuit() == 4) {
System.out.println(" of Diamonds");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.