这是演示课程的主要方法。
我的班级应该有以下变量:双倍宽度,高度, 它应该有这些方法: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:错误:构造函数 类Pyramid中的金字塔不能应用于给定类型; 金字塔卢克索=新金字塔(); ^ required:double,double found:无参数原因:实际和形式参数列表的长度不同
C:\ Users \ A1.D257 \ Desktop \ jaava \ egypt.java:15:错误:找不到 符号 Luxor.SetWidthHeight(W,H); ^符号:方法SetWidthHeight(double,double)location:变量类型为Pyramid的Luxor
C:\ Users \ A1.D257 \ Desktop \ jaava \ egypt.java:17:错误:找不到 符号 System.out.println(“Luxor的表面积为”+ getSurfaceArea()); ^符号:方法getSurfaceArea()location:class egypt 3 errors
工具已完成,退出代码为1
答案 0 :(得分: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第17行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;
}
}