2013-06-28 21 views
-2

它表示數字125,100是行中的不兼容類型:_die1 = newDie(125,100);我的編譯器說我有一個不兼容的類型。 Java

我不理解它,因爲在NewDie方法中它需要int 2整數,所以它應該工作正常...?!基本上這個程序創建擲骰,我試圖讓它顯示卷#2

import java.awt.*; 
// The panel which holds the two dice 
import javax.swing.*; 
public class DicePanel extends JPanel 
{ 
// instance variables 
private Die _die1, _die2; 
private int _roll; 


/** 
* Constructor for objects of class DicePanel 
*/ 
public DicePanel() 
{ 
    // initialise instance variables 
    super(); 
    setBackground(Color.GRAY); 



    _die1 = new Die(125,100); 
    _die2 = new Die(250,100); 



    roll(); 
} 
// display the dice in the panel 
public void paintComponent(Graphics pen) 
{ 
    super.paintComponent(pen); 
    Graphics2D aBetterPen = (Graphics2D)pen; 
    _die1.paint(aBetterPen); 
    _die2.paint(aBetterPen); 



} 
// roll both dice and display them 
public void roll() 
{ 


    // _die1 = new Two(125,100); 
    // _die2 = new Three(250,100); 

    _die1 = newDie(125,100); 
    // _die2 = DicePanel.newDie(250,100); 
    //repaint(); 
} 
// retrieve the value of each die 
public int getDie1() 
{ 
    return _die1.getValue(); 
} 
public int getDie2() 
{ 
    return _die2.getValue(); 
} 

// factory method for a die 
public void newDie(int x, int y){ 

    //_roll = randomNumber(1,6); 

    _die1 = new Two(x,y); 
    // _die2 = new Three(x,y); 

} 
// random number generator to return and integer between two integers, inclusive. 
public static int randomNumber(int low, int high){ 
    return low + (int)(Math.random()*(high-low+1)); 
} 

}

+0

有一種方法newDie – sashkello

+3

規則1)編譯器是正確的;規則2)如果編譯器錯誤,請參閱規則#1。 – user2246674

回答

3

newDie回報void這顯然是與_die的類型模具的不兼容。

1

沒有行說newDie(125,100)。然而,有一條線說new Die(125,100) ......這意味着非常不同的東西。它是一個構造函數調用,不是方法調用。

因此,要麼:

  • 您所呼叫的newDie方法,它是在抱怨,因爲newDie回報void,或者

  • 要調用的模具構造函數new Die(125, 100)以及正式和實際參數類型不匹配。

提供的錯誤消息是抱怨125100,我認爲第二個解釋是更可能的一種。但是你的問題中沒有包含Die構造函數聲明,所以我不能確定。


我還要補充一點,_die1_die2_roll根據 Java編碼風格......,特別是通過由甲骨文推薦的編碼風格編碼風格的侵犯。你有一個很好的理由不同(和個人偏好不是一個很好的理由),那麼你應該寫你的Java代碼,以符合主流風格。

你的代碼的縮進也與我見過的任何編碼風格不一致,也應該修復。

如果您純粹是爲了自己的利益編寫代碼,並且沒有其他人需要閱讀它,那麼您的代碼風格就是您自己的業務。但是如果你想要任何人(例如你的講師,你的同事,StackExchange讀者)閱讀它,那麼風格是重要的

相關問題