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

时间:2014-03-11 23:40:02

标签: java

我刚刚添加了构造函数Building,我认为一切都会正常工作,但我在第43行收到错误。当我创建对象Building b = new Building();时,它说我需要double参数中有1}}和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
}
}

2 个答案:

答案 0 :(得分:7)

你的问题就在这里Building b = new Building();//creates the object b

你的构造函数被设置为接受两个参数,一个double和一个int,但你都没有传递。

尝试使用类似的方法删除错误

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

也许更好的想法就是让一个没有参数的构造函数......

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

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

Confirmation that program compiles and runs

答案 1 :(得分:1)

我已修复并测试您的代码。现在跑。您需要在构造函数中添加两个参数(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
}
}