我必须要求用户输入两个盒子的尺寸,然后计算两个盒子的表面积和体积。我完成了一切,但我一直得到错误:类Box中的构造函数Box不能应用于给定类型:
我的主要课程BoxCalc:
package boxcalc;
import cs1.Keyboard;
public class BoxCalc
{
public static void main(String[] args)
{
int width, length, height, volume1, volume2, surfacearea1, surfacearea2;
Box b1 = new Box();
Box b2 = new Box();
b1.getClass();
b2.getClass();
// Ask the user the demensions for Box 1.
System.out.println ("Box 1");
System.out.print ("Enter a value for the height of Box 1: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 1: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 1: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 1.
volume1 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Ask the user the demensions for Box 2.
System.out.println ("Box 2");
System.out.print ("Enter a value for the height of Box 2: ");
height = Keyboard.readInt();
System.out.println ("Enter a value for the length of Box 2: ");
length = Keyboard.readInt();
System.out.println ("Enter a value for the width of Box 2: ");
width = Keyboard.readInt();
// Calulating the volume and the surface area of Box 2.
volume2 = width*height*length;
surfacearea2 = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
// Determain which Box has greater volume.
if (volume1 > volume2)
{
System.out.println("Box 1's volume is greater!");
}
else
if(volume2 > volume1)
{
System.out.println("Box 2's volume is greater!");
}
if(volume1 == volume2)
{
System.out.println("Box 1's volume is same as Box 2's!");
}
// Display all the information.
System.out.println();
System.out.println(b1);
System.out.println();
System.out.println(b2);
}
}
我的课程专栏:
package boxcalc;
public class Box
{
private int width, length, height, volume, surfacearea;
// Defining the width, length, and height.
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
// Method to calulate the volume.
public void findVolume(int volume)
{
volume = length * height * width;
}
// Method to calulate the surface area.
public void findSurfaceArea(int surfacearea)
{
surfacearea = (2*(length * height))+(2 *(length * width))+
(2*(width*height));
}
// Use toString method to display all the info above.
public String toString()
{
return ("Length: " + length + "\n" + "Width: " + width + "\n" +
"Height: " + height + "\n" + "Volume: " + volume +"\n" +
"Surface area: " + surfacearea + "\n");
}
}
答案 0 :(得分:2)
你的盒子构造函数:
public Box (int boxwidth, int boxlength, int boxheight)
{
width = boxwidth;
length = boxlength;
height = boxheight;
}
但您创建的框对象没有参数:
Box b1 = new Box();
Box b2 = new Box();
它应该是:
在您询问用户方框1的尺寸后,请输入:
Box b1 = new Box(width, length, height);
在你向用户询问方框2的尺寸后,请输入:
Box b2 = new Box(width, length, height);
答案 1 :(得分:1)
您使用 no 参数调用Box构造函数:Box b1 = new Box();
,并且在类本身中,它被定义为需要具有多个(三个)数字参数:public Box (int boxwidth, int boxlength, int boxheight)
。所以编译器正在抱怨某些事情不对劲。
答案 2 :(得分:1)
目前,您的程序不是面向对象的。
发布您的Box
课程毫无意义,因为您的主要课程没有以任何重要方式使用它。
通过在main中声明足够的变量来执行所有逻辑,您将以结构化编程方式确定哪个框更大。
您应该获得box1
的所有维度,然后执行以下操作:
Box b1 = new Box(width, height, length);
然后为box2
做同样的事情,再次:
Box b2 = new Box(width,height,length);
然后,在您的if
语句中,而不是volume1 > volume2
,您应该做更多这样的事情:
if(b1.getVolume() > b2.getVolume())
getVolume()
应该是Box
类的方法,如下所示:
public int getVolume() {
return volume;
}
(尽管最佳做法可能会说在volume
课程中甚至不包含Box
变量,而简单的做return (width * height * length);
同样的原则应该适用于您的计划的其他方面。
请注意,通过这种方式,您的主程序只需要3 int
个变量:width
,height
,length
。将b1
的维度发送到构造函数后,b1
变量就会知道其维度,您可以重复使用这些变量来获取b2
的维度。您不需要volume1
,volume2
,surfacearea1
或surfacearea2
个变量,因为完整的Box
类可以让您获得这些Box
通过getVolume()
和getSurfaceArea()
确定属性。