2013-03-11 80 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){ /..../} 
相关问题