我刚刚添加了构造函数Building,我认为一切都会正常,但是在第43行出现错误。创建对象Building b = new Building();时,它说我需要在参数中包含doubleint,所以我做到了就像它说的那样,但我只是不断收到更多错误。我究竟做错了什么?

// 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 a 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() // The 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() // This function prints outs the variables of the building
    {
        System.out.println("The area is: " + area + " feet squared");
        System.out.println("The number of stories 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); // The user enters either Y or y until
                                            // they wish to exit the program

        } while(ans == 'Y' || ans == 'y'); // Test of do/while loop
    }
}

最佳答案

您的问题是此行: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;
}
应用这些更改后,代码将编译并运行...(请参见下图)

关于java - Java错误: constructor in class cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22339041/

10-11 22:10
查看更多