我的代码有问题,因为它一直说构造函数是未定义的。我已经阅读过某处需要声明构造函数的参数。我只是不知道该怎么做。构造函数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