java - 我正在尝试为金字塔编写一个类定义。但由于某种原因它想要运行。我是初学者,非常迷失

标签 java

这是演示该类的主要方法。

我的类应该有这些变量:双倍宽度、高度、 它应该有这些方法:SetWidthHeight(double W, double H), GetVolume()。

     import java.util.*;


        public class egypt
        {
           public static void main(String[] args)
           {
              Scanner in = new Scanner(System.in);
              Pyramid Luxor = new Pyramid();
              double W, H;
              System.out.println("Enter Luxor's width:");
              W = in.nextDouble();
              System.out.println("Enter Luxor's height:");
              H = in.nextDouble();

              Luxor.SetWidthHeight(W, H);
              System.out.println("Luxor has volume of " + Luxor.GetVolume());
              System.out.println("Luxor has a Surface Area of " + getSurfaceArea());
           }
        }

       //This class describes pyramids with a square base.

        class Pyramid
        {
          private double Height;
          private double Width ;

          public Pyramid(double W, double H)
           {
                Height = H; Width = W;
        }

           public double GetVolume()
           {
              return Height * Width * Width / 3;
           }


           public double getSurfaceArea()
           {
              double sideLength = Math.sqrt(Height * Height
                 + Width * Width/ 4);
              return 2 * Width * sideLength;
           }
        }

错误

C:\Users\A1.D257\Desktop\jaava\egypt.java:8: error: constructor Pyramid in class Pyramid cannot be applied to given types; Pyramid Luxor = new Pyramid(); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length

C:\Users\A1.D257\Desktop\jaava\egypt.java:15: error: cannot find symbol Luxor.SetWidthHeight(W, H); ^ symbol: method SetWidthHeight(double,double) location: variable Luxor of type Pyramid

C:\Users\A1.D257\Desktop\jaava\egypt.java:17: error: cannot find symbol System.out.println("Luxor has a Surface Area of " + getSurfaceArea()); ^ symbol: method getSurfaceArea() location: class egypt 3 errors

Tool completed with exit code 1

最佳答案

您的编译器错误:

1) egypt.java 第 8 行 - Pyramid() 无法创建,因为您只有一个构造函数,并且需要两个参数

 public Pyramid(double W, double H)
 {
        Height = H; Width = W;
 }

2)egypt.java 第 15 行 - 您从未在 Pyramid.java 类中创建函数 SetWidthHeight()

3)egypt.java getSurfaceArea() 的第 17 行未在此范围内定义 - 只是一个小错误,您需要调用 Luxor.getSurfaceArea()

修复了错误,以准确地向您展示如何改进代码

import java.util.*;

// Class name should start uppercase
public class Egypt
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Pyramid luxor; // start with lowercase
        double width, height; // start with lowercase

        // Get input values
        System.out.println("Enter Luxor's width:");
        width = in.nextDouble();
        System.out.println("Enter Luxor's height:");
        height = in.nextDouble();

        // Initialize variable
        luxor = new Pyramid(width, height);

        System.out.println("Luxor has volume of " + luxor.getVolume());
        System.out.println("Luxor has a Surface Area of " + luxor.getSurfaceArea());
    }
}

// This class describes pyramids with a square base.
public class Pyramid
{
    private double height;
    private double width;

    public Pyramid(double width, double height)
    {
        this.height = height; this.width = width;
    }

    public double getVolume()
    {
        return height * width * width / 3;
    }


    public double getSurfaceArea()
    {
        double sideLength = Math.sqrt(height * height
                + width * width/ 4);
        return 2 * width * sideLength;
    }
}

关于java - 我正在尝试为金字塔编写一个类定义。但由于某种原因它想要运行。我是初学者,非常迷失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35880024/

相关文章:

java - 将文本字符串转换为替代表示形式(二进制、十六进制、ascii)

java - Android Studio/Gradle 项目中的纯 Java 8 库模块

java - Hibernate事务异常: nested transactions not supported

java - .put() 和 .accumulate() 追溯更改值的 JSON 行为

Java,如何让程序等待视频结束然后继续,wait()使程序停止

java - 无法在 Firebase、Android 上获取值

java - 用于检测重复值与另一个值的高效数据结构

java - Smack FileTransferManager.addFileTransferListener 从未被调用

java - While 循环无法正常工作

java - 在Java中从文件中读取数字/整数