Exception in thread “main“ java.lang.Error: Unresolved compilation problem: 这个错误意味着你在使用一个没有定义的构造函数

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
这个错误意味着你在使用一个没有定义的构造函数"求矩形面积1.Rectangle()"。你需要确定是否有一个名为Rectangle的构造函数可用,且没有参数。

要修复这个问题,你可以尝试以下几个步骤:

  1. 确保你有一个名为Rectangle的构造函数,并且没有参数。例如:public Rectangle() {}

  2. 确保你在创建Rectangle对象时使用了正确的构造函数。例如:Rectangle rectangle = new Rectangle();

  3. 如果你已经有了一个带有参数的构造函数,那么你需要根据需要调整代码,以适应带有参数的构造函数。

如果你仍然无法解决问题,请提供更多的代码和上下文,以便我们提供更具体的帮助。
代码中存在以下错误:

  1. 缺少import语句:缺少导入Scanner类的import语句。
  2. 类名不符合命名规范:add7应该改为Add7。
  3. Rectangle类没有提供无参构造方法:在main函数中创建Rectangle对象时,缺少必要的参数。
  4. main函数中没有调用任何方法或者输出结果:main函数没有实现任何功能。
    package xiaomingm;

import java.util.Scanner;

public class 求矩形面积1 {

class Rectangle{
    int height;
    int wight;

    public Rectangle(int height,int wight) {
        this.height = height;
        this.wight = wight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWight() {
        return wight;
    }

    public void setWight(int wight) {
        this.wight = wight;
    }
    public void getArea(){
        int area=wight*height;
        System.out.println("面积为:"+area);
    }
    public void getPer(){
        int per=(wight+height)*2;
        System.out.println("周长为:"+per);
    }
    public void show(){
        System.out.println("长为:"+height+"宽为:"+wight);
        getArea();
        getPer();
    }

}

public static void main(String[] args) {

    Rectangle add=new Rectangle();
}

}
以下是修改后的代码:

import java.util.Scanner;

public class Add7 {

    class Rectangle {
        int height;
        int wight;

        public Rectangle(int height, int wight) {
            this.height = height;
            this.wight = wight;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }

        public int getWight() {
            return wight;
        }

        public void setWight(int wight) {
            this.wight = wight;
        }

        public void getArea() {
            int area = wight * height;
            System.out.println("面积为:" + area);
        }

        public void getPer() {
            int per = (wight + height) * 2;
            System.out.println("周长为:" + per);
        }

        public void show() {
            System.out.println("长为:" + height + "宽为:" + wight);
            getArea();
            getPer();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入长方形的高度:");
        int height = scanner.nextInt();
        System.out.print("请输入长方形的宽度:");
        int width = scanner.nextInt();

        Add7 add = new Add7();
        Rectangle rectangle = add.new Rectangle(height, width);
        rectangle.show();
        
        scanner.close();
    }
}

修改后的代码解决了以上提到的问题,并在main函数中添加了用户输入,调用Rectangle对象的show函数来计算并输出长方形的面积和周长。

相关文章

文章浏览阅读2.2k次,点赞6次,收藏20次。在我们平时办公工作...
文章浏览阅读1k次。解决 Windows make command not found 和...
文章浏览阅读3.2k次,点赞2次,收藏6次。2、鼠标依次点击“计...
文章浏览阅读1.3w次。蓝光版属于高清版的一种。BD英文全名是...
文章浏览阅读974次,点赞7次,收藏8次。提供了更强大的功能,...
文章浏览阅读1.4w次,点赞5次,收藏22次。如果使用iterator的...