java错误:类中的构造函数不能应用于给定类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22339041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:02:18  来源:igfitidea点击:

java error: constructor in class cannot be applied to given types

java

提问by NJD

I just added the constructor Building and I thought everything would work fine, but I'm getting an error on line 43. When I create the object Building b = new Building();, it says I need to have a doubleand intin the argument, so I did as it said but I just keep getting more errors. What am I doing wrong?

我刚刚添加了构造函数 Building ,我认为一切都会正常工作,但是在第 43 行出现错误。当我创建对象时 Building b = new Building();,它说我需要在参数中有一个doubleand int,所以我按照它说的做了但是我只是不断收到更多错误。我究竟做错了什么?

//This program lets the user design the area and stories of a building multiple times
//Author: Noah Davidson
//Date: February 20, 2014

import java.util.*;

public class Building//class begins
{
static Scanner console = new Scanner(System.in);

double area;//attributes of building
int floors;

public Building (double squarefootage, int stories)
{
area = squarefootage;
floors = stories;
}

void get_squarefootage()//user enters the area of floor
{
System.out.println ("Please enter the square footage of the floor.");
area = console.nextDouble();
}

void get_stories()//user enters the amount of floors in the building
{
System.out.println ("Please enter the number of floors in the building.");
floors = console.nextInt();
}

void get_info()//function prints outs the vaibles of the building
{
System.out.println ("The area is: " + area + " feet squared");
System.out.println ("The number of stroies in the building: " + floors + " levels");
}

public static void main(String[] args)//main starts
{
char ans;// allows for char 

  do{ // do/while loop starts so user can reiterate the program as many times as they desire
   Building b = new Building();//creates the object b
   b.get_squarefootage();//calls the user to enter the area
   b.get_stories();//calls the user to enter the floors
   System.out.println ("---------------");
   b.get_info();// displays the variables
   System.out.println ("Would you like to repeat this program? (Y/N)");
   ans = console.next().charAt(0);// user enters either Y or y until they wish to exit the program
   }while(ans == 'Y'||ans == 'y');// test of do/while loop
}
}

采纳答案by Josh Engelsma

Your problem is this line here Building b = new Building();//creates the object b

你的问题是这里的这一行 Building b = new Building();//creates the object b

Your constructor is set up to take two arguments, a double and an int, but you pass neither.

您的构造函数设置为采用两个参数,一个 double 和一个 int,但您都没有传递。

Try something like this to remove error

尝试这样的事情来消除错误

double area = 0.0;
int floors = 0;
Building b = new Building(area, floors);

Perhaps a better idea would be to just have a constructor that took no parameters...

也许更好的主意是只拥有一个不带参数的构造函数......

public Building{
    this.area = 0.0;
    this.floors = 0;
}

After I apply these changes, the code compiles and runs... (see picture below)

应用这些更改后,代码将编译并运行...(见下图)

Confirmation that program compiles and runs

确认程序编译并运行

回答by Neptune

I have fix and test your code. Now run. You need to add two argument to the constructor (double and int).

我已经修复并测试了您的代码。现在运行。您需要向构造函数添加两个参数(double 和 int)。

import java.util.*;

public class Building//class begins
{
static Scanner console = new Scanner(System.in);

double area;//attributes of building
int floors;

public Building (double squarefootage, int stories)
{
area = squarefootage;
floors = stories;
}

void get_squarefootage()//user enters the area of floor
{
System.out.println ("Please enter the square footage of the floor.");
area = console.nextDouble();
}

void get_stories()//user enters the amount of floors in the building
{
System.out.println ("Please enter the number of floors in the building.");
floors = console.nextInt();
}

void get_info()//function prints outs the vaibles of the building
{
System.out.println ("The area is: " + area + " feet squared");
System.out.println ("The number of stroies in the building: " + floors + " levels");
}

public static void main(String[] args)//main starts
{
char ans;// allows for char 

  do{ // do/while loop starts so user can reiterate the program as many times as they desire
   double a =1;
   int c = 2;
   Building b = new Building(a,c);//creates the object b
   b.get_squarefootage();//calls the user to enter the area
   b.get_stories();//calls the user to enter the floors
   System.out.println ("---------------");
   b.get_info();// displays the variables
   System.out.println ("Would you like to repeat this program? (Y/N)");
   ans = console.next().charAt(0);// user enters either Y or y until they wish to exit the program
   }while(ans == 'Y'||ans == 'y');// test of do/while loop
}
}