簡體   English   中英

為什么我不能調用新多邊形?

[英]Why can't I call new polygon?

我的老師給了我這個

  1. 在-邊正多邊形中,所有邊的長度相同,所有角的度數都相同。 設計一個名為 RegularPolygon 的 class 包含: 一個名為 n 的私有 int 數據字段,它定義 Polygon 中默認值為 3 的邊數。 一個名為 side 的私有雙精度數據字段,用於存儲默認值為 1 的邊的長度。名為 X 的雙精度數據字段,用於定義多邊形中心的 x - 坐標,默認值為 0。名為 Y 的私有雙精度數據字段,用於定義多邊形中心的 y - 坐標,默認值為 0。創建正多邊形的構造函數指定的邊數、邊長和 x - 和 y- 坐標(值從參數傳遞到字段)。 所有數據字段的訪問器方法。 getPerimeter() 方法返回多邊形的周長。 getArea() 方法返回多邊形的面積。 公式為面積 = n * s*s / (4 * tan(PI / n))。

2) 編寫一個RegularPolygonTest class,允許用戶輸入數據字段,你的程序打印出正多邊形的周長和面積。

到目前為止,這是我的代碼:

public class RegularPolygon{
    private int n;
    private double side, x, y;
    public RegularPolygon(){
        n = 3;
        side = 1;
        x = 0; 
        y = 0;
    }

    public RegularPolygon(int n, double side){
        this.n = n;
        this.side = side;
        x = 0;
        y = 0;
    }
    public RegularPolygon(int sn, double length, double x_coord, double y_coord){
        n = sn;
        side = length;
        x = x_coord;
        y = y_coord;
    }

    //set n to the user input
    public void setN(int other){
            n = other;
    }
    public int getN(){
        return n;
    }
    //set side to userinput
    public void setSide(double otherside){
        side = otherside;
    }
    public double getSide(){
        return side;
    }
    //set x to user input
    public void setX(int x_co){
        x = x_co;
    }
    public double getX(){
        return x;
    }
    //set y to user input
    public void setY(int they){
        y = they;
    }
    public double getY(){
        return y;
    }
//find perimeter
    public double getPerimeter(){
        return n * side;
    }
//find area
    public double getArea(){
        double s_squ = side * side;
        double pin = Math.PI/n;
        double tangent = Math.tan(pin);
        return (n*s_squ)/(4*tangent);
    }

}
import java.util.Scanner;
public class RegularPolygonTest{
    public static void main(String[] args){
        Scanner yer = new Scanner(System.in);
        //number of sides
        System.out.println("Enter number of sides: ");
        int sn = yer.nextInt();

        //length of sides
        System.out.println("Enter length of sides: ");
        double length = yer.nextDouble();

        //x-coord
        System.out.println("Enter the x-coordinate of the center: ");
        double x_coord = yer.nextDouble();

        //y-coord
        System.out.println("Enter the y-coordinate of the center: ");
        double y_coord = yer.nextDouble();

        if (x_coord == 0 && y_coord == 0){
            RegularPolygon rp = new RegularPolygon(sn, length);
        }
        else if (sn > 3 && length > 1){
            RegularPolygon rp = new RegularPolygon(sn, length, x_coord, y_coord); 

        }
        else{
            RegularPolygon rp = new RegularPolygon();
        }
        System.out.println("The perimeter of the " + rp.getN() + "-sided polygon is : "+ rp.getPerimeter() +". And the are is : "+ rp.getArea());
    }

}

我得到的錯誤是 IDE 找不到符號,它指向最后一行中的所有 rp。 我該如何解決這個錯誤?

所有的 rp 都在塊內。 您需要在 ifs 之前定義一個可能未初始化的 rp,並在 ifs 中使用這個公共 rp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM