Java 计算矩形的周长和面积

Java 计算矩形的周长和面积,java,Java,我需要能够在控制台中输入矩形的长度和宽度,并计算其周长和面积。我让它工作,而不是接受我的输入进行计算。我知道我很接近,但似乎无法理解。提前感谢你的帮助。请记住,我是一个新手,说得好听一点,所以你的答案一开始可能对我没有意义。我无法让它计算输入控制台的值 package edu.purdue.cnit325_lab1; public class Rectangle { private static double length; private static double

我需要能够在控制台中输入矩形的长度和宽度,并计算其周长和面积。我让它工作,而不是接受我的输入进行计算。我知道我很接近,但似乎无法理解。提前感谢你的帮助。请记住,我是一个新手,说得好听一点,所以你的答案一开始可能对我没有意义。我无法让它计算输入控制台的值

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);
    }
}

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

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();
更新:您不会像另一个答案所指出的那样,将
L
W
传递给构造函数。但是,您犯了一些错误:

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

注意,调用矩形构造函数时没有参数,因此应使用

矩形单元矩形=新矩形(L,W)

实际上,就像另一个答案一样,您应该使用一个Scanner实例


另外关于编码风格:不要对变量名进行大写。对于“经验丰富”的java开发人员来说,这相当令人困惑。:-)

您没有调用
参数化构造函数

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);
}

注意:在代码中不必要地创建了两个
扫描仪
对象和两个
矩形
对象,这些对象将从上述代码中删除。

因此需要以某种方式设置值。。。你可以做任何一件事

(A)

(B)

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

setLength(double l) length = l;
setWidth(double w) width = w

double getLength() return length;
double getWidth() return height;
因为您正在使用默认构造函数初始化

阿卡


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

您的代码由默认构造函数组成,默认构造函数将根据您的设置将相应的长度和宽度值初始化为
0.0
,还包括参数化构造函数,该构造函数要求提供输入值并相应地设置值

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

Rectangle unitRectangle=新矩形()

因此,将它们设置为0.0

如果你这样做

Rectangle unitRectangle2=新矩形(2.3,4.3)


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

您必须告诉我们您面临的问题是什么?第一部分来自rectangle.java,第二部分来自testrectangle.java。我看得见。但是你的代码有什么问题?它怎么不起作用?您是否收到任何错误、意外输出?@RohitJain在运行程序时,当我在控制台中输入值时,它将不会接受并计算周长和面积。@RohitJain无论我提供什么输入,我都会得到相同的输出。请输入矩形的长度:3请输入矩形的长度:3单位矩形的面积为0.0单位矩形的权限为0。0@MartinCourtreaux我已经这样做了,但它仍然产生同样的结果。请输入矩形的长度:3请输入矩形的长度:3单位矩形的面积为0.0单位矩形的允许值为0.0您没有将读取的矩形的宽度和高度传递给构造函数。@Martincourtaux,我认为这是个问题,但我不知道如何补救,因为我正在使用两个构造函数(如果这是正确的术语)RectangleArea和RectanglePerimiter。非常感谢您的输入,但是根据分配,如果没有提供输入,我们必须默认为零。感谢您对大写字母问题的见解。在上一节C#课上,我被教导在分配姓名时使用驼峰式大小写。我应该更具体一些。你从哪里得到单位矩形?谢谢,这已经解决了。我知道我很接近,但作为一个全新的编码新手,我不确定我错过了什么。非常感谢您的帮助。如果您想要默认值,请使用scanner.hasNextDouble(),如果为true,请通过netDouble()获取double,否则使用0作为值。所以像double d=scanner.hasNextDouble()这样的东西?scanner.nextDouble():0.0;这就解决了这个问题,但是由于User272119首先发布了我不得不接受他的答案。这是一个用JAVA计算矩形面积和周长的程序,下面也是用c写的。
//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();
        }
    }
}
 Rectangle unitRectangle = new Rectangle(); 
//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();
        }
    }
}
//This is the Java code for finding the area and perimeter of a rectangle
package demo;

import java.util.Scanner;

public class DemoTranslation {
public static int area(int length, int width) {
    int areaOfRectangle;
    areaOfRectangle = length * width;
    System.out.println("Area of Rectangle is : " + areaOfRectangle);
    return 0;
}

public static int perimeter(int length, int width) {
    int perimeterOfRectangle;
    perimeterOfRectangle = (length + width) * 2;
    System.out.println("Perimeter of Rectangle is : " + perimeterOfRectangle);
    return 0;
}

public static void main(String[] args) {
    int length, width, choice;
    System.out.println("Enter the length of the triangle ");
    length = STDIN_SCANNER.nextInt();
    System.out.println("Enter the width of the triangle ");
    width = STDIN_SCANNER.nextInt();
    System.out.println("Enter 1 : View the area ");
    System.out.println("Enter 2 : View the perimeter ");
    System.out.println("Enter 3 : view both ");
    choice = STDIN_SCANNER.nextInt();
    switch(choice) {
    case 1:
        area(length, width);
        break;
    case 2:
        perimeter(length, width);
        break;
    case 3:
        area(length, width);
        perimeter(length, width);
        break;
    default:
        System.out.println("Invalid option ");
        break;
    }
    }

    public final static Scanner STDIN_SCANNER = new Scanner(System.in);
    }