iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >Exception in thread “main“ java.lang.Error: Unresolved compilation problem: 这个错误意味着你在使用一个没有定义的构造函数
  • 318
分享到

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

java开发语言 2023-09-25 09:09:36 318人浏览 独家记忆
摘要

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函数来计算并输出长方形的面积和周长。

来源地址:https://blog.csdn.net/zezeaichirou/article/details/132559506

--结束END--

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

本文链接: https://www.lsjlt.com/news/417263.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作