(初学者Java):构造函数没有参数?

时间:2016-04-07 15:17:56

标签: java constructor arguments

我正在尝试创建一个简单的程序,它取三个数字的平均值,但我得到一个错误,上面写着说

“类平均值中的构造函数平均值不能应用于给定类型;   必需:没有参数   found:int,int,int“

这是我的代码:

public class ave {
 public static void main(String args[]) {
average object = new average(3,4,6);
 }
}

这是我的构造函数代码

public class average {
public double takeaverage(double first, double second, double third) {
 double ave = (first + second + third)/3;
 System.out.println(ave);
 return ave; } 
} 

5 个答案:

答案 0 :(得分:0)

构造函数的名称应与类名相同,并且功能没有返回类型。 请尝试以下方法:

public class ave {
 public static void main(String args[]) {
Average object = new Average(3,4,6);
 }
}


public class Average {
    public Average(double first, double second, double third){
        double ave = (first + second + third)/3;
        System.out.println(ave);
    } 
} 

如果您不想更改类Average的代码,那么只需按照以下方法调用该类中的方法:

public class ave {
 public static void main(String args[]) {
Average object = new Average();
 double Avg = object.takeaverage(3,4,6);
 }
}


public class Average {
public double takeaverage(double first, double second, double third) {
 double ave = (first + second + third)/3;
 System.out.println(ave);
 return ave; } 
} 

答案 1 :(得分:0)

构造函数必须与类名相同。

你必须创建:

{{ HTML::row() }}
  {{ HTML::col(12, 12, 12, 12) }}
  <h3>{{ $section->Description}}</h3>
  <h4>{{ $section->fkey_CourseCodeId }}- {{ $section->Number }}- {{ $section->ClassSection }}</h4>
  <th>Number</th>
  <th>Section</th>
  <th>Semester</th>
    {{ HTML::table(array('class' => 'table table-striped table-bordered table-hover table-condensed datatable')) }}
      <thead>
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
          @foreach($rubrics as $rubric)
            @foreach($rubric->metric_category->metrics as $metric)
              <th>{{ $metric->metric_text }}</th>
            @endforeach
          @endforeach
        </tr>
      </thead>
      <tbody>
        @foreach($registrations as $registration)
          <tr>
            <td></td>
            <td>{{$registration->RCID}}</td>
          </tr>
        @endforeach
      </tbody>
    {{ HTML::closeTable() }}
  {{ HTML::closeCol() }}
{{ HTML::closeRow() }}

实际上,类中唯一存在的构造函数是没有参数的构造函数,它是自动创建的。

答案 2 :(得分:0)

您尚未定义接收这些参数的构造函数方法(3,4,6)。 你必须创建一个这样的构造函数方法:

public class average {
public double result

public average(int a, int b, int c){
this.result=this.takeaverage(double(a),double(b),double(c))
}
public double takeaverage(double first, double second, double third) {
 double ave = (first + second + third)/3;
 System.out.println(ave);
 return ave; } 
} 

答案 3 :(得分:0)

如果您只想取3个数字的平均值,则无需创建平均对象即可。我在下面展示了它:

import java.util.*;
public class Ave {
    public static void main(String[] args){
        double number1, number2, number3;
        Scanner console = new Scanner(System.in);
        System.out.println("Enter number 1:");
        number1 = console.nextDouble();
        System.out.println("Enter number 2:");      
        number2 = console.nextDouble();
        System.out.println("Enter number 3:");
        number3 = console.nextDouble();
        double average = (number1 + number2 + number3)/3;
        System.out.println("The average is: " + average);
    }
}

上述程序会询问用户3个数字并打印出他们的平均值。请注意,它仍然有点多余。您也可以修改它以询问任何数字的平均值,如下所示:

import java.util.*;
public class Ave {
    public static void main(String[] args){
        int howMany;
        double sum = 0.0;
        double number, average;
        Scanner console = new Scanner(System.in);
        System.out.println("How many numbers do you want to take average of:");
        howMany = console.nextInt();
        int count = 1;
        while(count <= howMany){
            System.out.println("Enter number " + count);
            number = console.nextDouble();
            sum += number;
            count++;
        }
        average = sum/howMany;
        System.out.println("The average is: " + average);
    }
}

您可以修改程序以获取3个数字的平均值,而无需询问用户,其中number1,number2和number3的双精度数将被赋予(硬编码,这通常不是一个好习惯)。 / p>

答案 4 :(得分:0)

您已创建“takeaverage”方法但尚未调用它。也许您应该使用setter或使用构造函数传递三个数字并初始化,然后只调用“takeaverage”方法获取double值并获得分配给该double变量的平均值。

您需要使用默认构造函数并从“average”类创建对象。以下是一个非常简单的解决方案:

public class average {
     public double takeaverage(double first, double second, double third) {
         double ave = (first + second + third)/3;
         System.out.println(ave);
         return ave; 
     } 
} 




 //main class
    public class ave{
      public static void main(String []args){
            average object = new average();
            object.takeaverage(3,4,6);
         }

    }