我的代碼有問題,因爲它一直說構造函數是未定義的。我已經閱讀過某處需要聲明構造函數的參數。我只是不知道該怎麼做。構造函數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);
}
}
錯誤是在這裏 方法輸入=新方法(大小,startV,endingV);方法1打印=新方法1(大小,數組); – 2013-03-11 20:35:24
不要使用'double Difference' - 保持變量和方法名稱爲camelCase並使用PascalCase作爲類名稱。例如,「雙重差異」。 (請注意StackOverflow如何將「Difference」的顏色更改爲藍色?這是因爲按照慣例,在Java中,以大寫字母開頭的單詞被視爲類。)另外,請避免使用單詞「Method」作爲類的名稱 - - 這很混亂。 – 2013-03-11 20:37:05