计算矩形的周长和面积

我需要能够将矩形的长度和宽度输入控制台并计算其周长和面积。 除了接受我的计算输入之外,我还有其他工作。 我知道我很亲密,但似乎无法弄明白。 在此先感谢您的帮助。 请记住,我是一个很好的新手,所以你的答案起初可能对我没有意义。 我无法计算我输入控制台的值。

package edu.purdue.cnit325_lab1; public class Rectangle { private static double length; private static double width; public Rectangle() { length=0.0; width=0.0; } public Rectangle(double l, double w) { length = l; width = w; } public double FindArea() { return length*width; } public double FindPerim() { return length*2 + width*2; } } package edu.purdue.cnit325_lab1; import java.util.Scanner; public class TestRectangle { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanL = new Scanner (System.in); System.out.print("Please enter the length of the rectangle: "); double L = scanL.nextDouble(); Scanner scanW = new Scanner (System.in); System.out.print("Please enter the length of the rectangle: "); double W = scanW.nextDouble(); //int W = scanW.nextInt(); double RectangleArea; Rectangle unitRectangle = new Rectangle(); RectangleArea = unitRectangle.FindArea(); System.out.println("The area of a unit rectangle is " + RectangleArea); double RectanglePermiter; Rectangle perimRectangle = new Rectangle(); RectanglePermiter = perimRectangle.FindPerim(); System.out.println("The permimiter of the unit rectangle is " + RectanglePermiter); } } 

请注意,您正在调用不带参数的Rectangle构造函数,因此将其宽度和高度设置为零,您应该使用

Rectangle unitRectangle = new Rectangle(L,W);

而且就像其他答案一样,你应该使用一个Scanner实例。

另外关于编码风格:不要使用变量名称。 对于更有“经验丰富”的Java开发人员而言,它相当混乱 🙂

你错过了调用parameterized constructor

 public static void main(String[] args) { Scanner scanL = new Scanner (System.in); System.out.print("Please enter the length of the rectangle: "); double L = scanL.nextDouble(); System.out.print("Please enter the length of the rectangle: "); double W = scanL.nextDouble(); Rectangle rectangle = new Rectangle(l,w); double rectangleArea = rectangle .FindArea(); System.out.println("The area of a unit rectangle is " + rectangleArea); double rectanglePermiter = rectangle.FindPerim(); System.out.println("The permimiter of the unit rectangle is " + rectanglePermiter); } 

注意:您必须在代码中创建两个Scanner对象和两个Rectangle对象,这些对象将从上面的代码中删除。

使用一个扫描仪实例。 只需重复使用它。

 Scanner scanner = new Scanner (System.in); System.out.print("Please enter the length of the rectangle: "); double L = scanner.nextDouble(); System.out.print("Please enter the length of the rectangle: "); double W = scanner.nextDouble(); 

更新:您没有像其他答案所指出的那样将LW传递给构造函数。 但是,你犯了一些错误:

  • 您将lengthwidth声明为static 。 不要那样做。 这是没有意义的。 长度和宽度是矩形的属性,不应由所有矩形实例共享。
  • 您没有使用正确的命名约定:变量以小写字符开头,类名以大写字符开头。
  • 您正在创建Rectangle的两个实例,以计算同一矩形的周长和面积。 分享该实例。

所以你需要以某种方式设置值……你可以做到

一个)

 Rectangle unitRectangle = new Rectangle(l,w); 

B)

或者在矩形类中创建getter和setter ..

 setLength(double l) length = l; setWidth(double w) width = w double getLength() return length; double getWidth() return height; 

由于您使用默认构造函数进行初始化

又名

  Rectangle unitRectangle = new Rectangle(); 

长度和宽度的值也将为零。

您的代码由Default构造函数组成,它将根据您的设置将相应的值length和width初始化为0.0 ,并且还包含需要提供输入值的参数化构造函数,并相应地设置值。

在创建类的对象时,您将调用默认构造函数而不是此行中的参数化构造函数

Rectangle unitRectangle = new Rectangle();

因此将它们设置为0.0

如果你做这样的事情

Rectangle unitRectangle2 = new Rectangle(2.3,4.3);

这将创建具有长度和宽度值的对象,分别为2.3和4.3。

 //write a java program which will calculate area and perimeter of rectangle by accepting radius from cmd prompt //area = width x height,perimeter = (2 x width) + (2 x height) class AreaAndPerOfRect { int h,w; void set(int x,int y) { h=x; w=y; } int AreaOfRect() { int area=w*h; return area; } int PerOfRect() { int per=(2*w)+(2*h); return per; } void disp() { int area=AreaOfRect(); System.out.println("area of rectangle"+area); int per=PerOfRect(); System.out.println("area of rectangle"+per); } } class AreaAndPerOfRectDemo { public static void main(String args[]) { if(args.length!=2) { System.out.println("please enter two values"); } else { int x=Integer.parseInt(args[0]); int y=Integer.parseInt(args[1]); AreaAndPerOfRect ap=new AreaAndPerOfRect(); ap.set(x,y); ap.disp(); } } }