2013-01-31 25 views
1

我正在製作一個繪製原子衰變的程序。這是我的主,這也是邏輯。但是,如果在其他類中明確定義了錯誤,則會收到未定義的構造函數錯誤。這是爲什麼發生?構造函數是未定義的,當它被明確定義時

注意:它沒有標出。讓我發怒。

import java.util.Random; 
import java.awt.*; 
import javax.swing.*; 
import javax.swing.JFrame; 

public class Main { 
    public static void main(String args[]) { 
     int chance = 6; 
     Random r = new Random(); 
     int num = 40; 
     int[] decayed; 
     int reps = 25; 
     decayed = new int[reps]; 
     for (int j = 1; j < reps+1; j++) { 
      for (int i = 0; i < num; i++) { 
       int c = r.nextInt(chance); 
       if (c == chance - 1) { 
        decayed[j]++; 
       } 
      } 
      System.out.printf("\n Trial: " + j + "\n Number left: " + num 
        + "\n Decayed: " + decayed[j] + "\n\n"); 
      num = num - decayed[j]; 

     } 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.getContentPane().add(new Graph(decayed[])); //"Constuctor is undefined for type int" When I am clearly specifying an array. 
     f.setSize(400,400); 
     f.setLocation(200,200); 
     f.setVisible(true); 
    } 
} 

和我的Graph.class。它是從一些論壇複製的(Credit to Crieg Wood)。

import java.awt.*; 
import javax.swing.*; 
import java.util.*; 
public class Graph extends JPanel 
{ 
    int PAD = 20; 
    boolean drawLine = true; 
    boolean drawDots = true; 
    int dotRadius = 3; 

    // the y coordinates of the points to be drawn; the x coordinates are evenly spaced 
    int[] data; 
    public Graph(int points[]){ //This is the constructor which specifies type int[]. 
     for (int i = 0; i<points.length; i++){ //Copies points[] to data[] 
      data[i] = points[i]; 
     } 
    } 

    protected void paintComponent (Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     int w = getWidth(); 
     int h = getHeight(); 
     g2.drawLine(PAD, PAD, PAD, h-PAD); 
     g2.drawLine(PAD, h-PAD, w-PAD, h-PAD); 
     double xScale = (w - 2*PAD)/(data.length + 1); 
     double maxValue = 100.0; 
     double yScale = (h - 2*PAD)/maxValue; 
     // The origin location 
     int x0 = PAD; 
     int y0 = h-PAD; 

     // draw connecting line 
     if (drawLine) 
     { 
      for (int j = 0; j < data.length-1; j++) 
      { 
       int x1 = x0 + (int)(xScale * (j+1)); 
       int y1 = y0 - (int)(yScale * data[j]); 
       int x2 = x0 + (int)(xScale * (j+2)); 
       int y2 = y0 - (int)(yScale * data[j+1]); 
       g2.drawLine(x1, y1, x2, y2); 
      } 
     } 

     // draw the points as little circles in red 
     if (drawDots) 
     { 
      g2.setPaint(Color.red); 
      for (int j = 0; j < data.length; j++) 
      { 
       int x = x0 + (int)(xScale * (j+1)); 
       int y = y0 - (int)(yScale * data[j]); 
       g2.fillOval(x-dotRadius, y-dotRadius, 2*dotRadius, 2*dotRadius); 
      } 
     } 
    } 
} 
+4

失去[ ] from'decayed []' – Thomas

+0

你真的得到'未定義'的錯誤嗎?你應該得到一個語法錯誤。這不是一回事。 – EJP

+0

檢查http://sscce.org/ –

回答

4

你的語法是無效的,是指你的陣列,只需使用變量名,沒有[]

f.getContentPane().add(new Graph(decayed)); 
1

只需更換這f.getContentPane().add(new Graph(decayed[]));

與此

f.getContentPane().add(new Graph(decayed));

只需使用您沒有創建的變量的名稱[]

那些[]括號僅在數組的方法參數聲明時使用,而不是在調用方法時使用。

8

這裏的問題與這些[]括號的用法有關。

嘗試重新寫您的來電:

f.getContentPane().add(new Graph(decayed)); 

雖然你不是不正確,請考慮重新寫你的構造函數來保存對Java標準和約定:

public Graph(int[] points){ // NOTE: I moved the [] to a the standard position 
    for (int i = 0; i<points.length; i++){ 
     data[i] = points[i]; 
    } 
} 
+1

@ theJollySin'[]'在雙方都允許,所以你爲什麼在回答中提到 – Abubakkar

+5

@阿布原諒我迂腐,但我發現遵循標準和一種語言的慣例使每個人都更容易生活。 – theJollySin

+0

是的,這是真的,但這可能會使OP認爲OP在解決方案中使用它的方式是錯誤的。相反,你應該提到(明確指出)它是一個慣例 – Abubakkar

相關問題