当前位置: 首页 > 知识库问答 >
问题:

我试图为金字塔写一个类定义。但出于某种原因,它想要运行。我是初学者,非常迷茫

东和怡
2023-03-14

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

我的类应该有以下变量:double Width,Height,它应该有以下方法: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: \用户\ A1。D257\Desktop\jaava\egypt。java:8:错误:类金字塔中的构造函数金字塔不能应用于给定类型;金字塔Luxor=新金字塔();^必需:double,double found:无参数原因:实际参数列表和形式参数列表长度不同

C: \用户\ A1。D257\Desktop\jaava\egypt。java:15:错误:找不到符号Luxor。设置宽度高度(W,H);^符号:方法SetWidthHeight(double,double)位置:金字塔类型的变量Luxor

C:\用户\A1. D257\桌面\jaava\egypt.java:17:错误:找不到符号System.out.println("Luxor有一个表面积"getSurfaceArea()); ^ 符号:方法getSurfaceArea()位置:类埃及3错误

工具已完成,退出代码为1

共有1个答案

督冠玉
2023-03-14

您的编译器错误:

1) 埃及8号线。无法创建java-Pyramid(),因为您只有一个构造函数,它需要两个参数

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

2) 埃及15号线。java-您从未在类Pyramid中创建函数SetWidthHeight()。Java语言

3) 埃及17号线。java getSurfaceArea()不在此范围内定义,只是在调用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;
    }
}
 类似资料: