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

新手上路,请多包涵

我刚刚添加了构造函数 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
    }
}

原文由 NJD 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 588
2 个回答

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

您的构造函数设置为接受两个参数,一个双精度参数和一个整数参数,但您没有传递任何参数。

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

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

也许更好的主意是只使用一个不带参数的构造函数:

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

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

确认程序编译运行

原文由 Josh Engelsma 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

 import java.util.*;

public class Building // The 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() // The 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 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); // The user enters either Y or y until
                                            // they wish to exit the program

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

原文由 Neptune 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏