2013-03-11 142 views
1

我的代碼有問題,因爲它一直說構造函數是未定義的。我已經閱讀過某處需要聲明構造函數的參數。我只是不知道該怎麼做。構造函數undefined

如果有人可以幫忙,我是新來的java和編程。我的代碼如下:

import java.util.*;//import library 



class Input 
{ 

    public Input (int size,int startV,int endingV) 
     { 

     //declarations of variables 
     double difference; 
     double[] array= new double[size]; 

     array[0]=startV; 

    //calculating the difference to add on each number in the array 
    difference=(endingV-startV)/size; 

    for (int counter=1;counter<size;counter++) //for loop to fill the array 
       { 
     array[counter]=array[counter-1] + difference;   
      } 


     } 

    public Input enter(int size,int startV,int endingV) 
     { 

     //declarations of variables 
     double difference; 
     double[] array= new double[size]; 

     array[0]=startV; 

      //calculating the difference to add on each number in the array 
     difference=(endingV-startV)/size; 

      for (int counter=1;counter<size;counter++) //for loop to fill the array 
     { 
      array[counter]=array[counter-1] + difference;   
     } 

      return this; 
    } 
} 

class Show 
{ 
    public Show (int size,double[] array) 
    { 

     for (int i=0;i<size;i++) //for loop to print the array 
      System.out.println("This is the array " + i+ ": " + array[i]); 

    } 

    public Show print(int size,double[] array) 
    { 

     for (int i=0;i<size;i++) //for loop to print the array 
     System.out.println("This is the array " + i+ ": " + array[i]); 

     return this; 
    } 
} 

public class Assignment2 
{ 

    public static void main(String[] args) 
    { 
     //declaring variables 
     int startV,endingV; 
     int size=0; 



     System.out.print("Give the size of the array:");//Print message on screen 
     size = new Scanner(System.in).nextInt();//asking for the size of array 

      double[] array= new double[size]; //creation of array 

    System.out.print("Give the starting value of the array:"); 
    startV = new Scanner(System.in).nextInt();//asking for the starting value of array 

    System.out.print("Give the ending value of the array:"); 
    endingV = new Scanner(System.in).nextInt();//asking for the last value of array 

    //calling the functions from the other classes 

     Input enter= new Input(size,startV,endingV); 
     Show print= new Show(size,array); 

    } 



} 
+0

錯誤是在這裏 方法輸入=新方法(大小,startV,endingV);方法1打印=新方法1(大小,數組); – 2013-03-11 20:35:24

+0

不要使用'double Difference' - 保持變量和方法名稱爲camelCase並使用PascalCase作爲類名稱。例如,「雙重差異」。 (請注意StackOverflow如何將「Difference」的顏色更改爲藍色?這是因爲按照慣例,在Java中,以大寫字母開頭的單詞被視爲類。)另外,請避免使用單詞「Method」作爲類的名稱 - - 這很混亂。 – 2013-03-11 20:37:05

回答

0

你需要創建一個

public Method(size,startV,endingV) 

public Method enter = (size, startV, endingV) 

首先是一個構造第二個是方法

4

你靠近:

你有一個方法:

public Method enter(int size,int startV,int endingV) { 

,使其構造它的簽名必須是

public Method (int size,int startV,int endingV) { 

,然後你必須刪除return this;聲明。

請記住,構造函數沒有返回類型,並且它們的名稱與該類的名稱相同。有了這些信息,您還可以修復構造函數Method1


此外,請尊重Java命名約定,並且使變量以小寫字母開頭,以提高代碼的可讀性。

+0

好吧,我修復了那個,但現在我有一個新的錯誤。我的數組返回0值。在我把所有東西放在類中之後,我讓它們在函數中,並且程序打印的是正確的值。現在我得到: 給出數組的大小:10 給出數組的起始值:10 給出數組的結尾值:210 這是陣列0:0.0 這是陣列1:0.0 這是陣列2:0.0 這是陣列3:0.0 這是陣列4:0.0 這是陣列5:0.0 這是數組6:0.0 這是數組7:0.0 這是數組8:0.0 這是數組9:0.0 – 2013-03-11 20:56:43

+0

@AntreasSolou:很高興提供幫助。這超出了您發佈的問題的範圍。如果你不知道爲什麼它發生(用調試器)隨時發佈一個新的問題:) – jlordo 2013-03-11 20:58:28

+0

@AntreasSolou:你應該嘗試調試之前張貼在這裏。你的下面的問題是在我評論這裏8分鐘後。這並不表明你第一次嘗試解決你的問題... – jlordo 2013-03-11 21:43:01

0

您的構造函數必須具有與您的類相同的名稱並且沒有返回類型。因此,對於你Method類,構造函數將只是:

public Method(int size, int startV, int endingV) 
{ 
    // code... 
} 

也請注意,構造器是存在於初始化對象的情況下,如果你想創建一個具有特定演算的方法,那麼,你」馬上要做的:

public int enter(int size, int startV, int endingV) 
{ 
    int result = 0; 
    // code to calculate, for example result = size + startV + endingV ... 
    return result; 
} 
0

對於類方法

默認構造函數將

public Method(){} 

對於類方法一

默認構造函數將

public Method1(){} 
類中的

有沒有構造爲

構造函數的名稱必須是將相同的類名。

enter(int size,int startV,int endingV) 

print(int size,double[] array) 

可以在你的類的兩種方法。

也是你的兩個構造可 -

public Method(int size,int startV,int endingV){ /..../} 

public Method1(int size,double[] array){ /..../}