新手java作业,创建一个box输入长宽高求面积体积
大家好,我是java新手,这个作业马上要交了,不知道为什么摁‘2’的时候第二个长方体的代码run出不来,有问题的一段我用红色标出了,请高人指点 :) 不胜感激!谢谢
刘丽~
public class lliuBox {
private double length;
private double height;
private double width;
lliuBox(){
length = 2.0;
height = 2.0;
width = 2.0;
}
lliuBox(double l, double h, double w){
this.length = l;
this.height = h;
this.width = w;
}
public void printBox(){
System.out.println("length: " + length);
System.out.println("height: " + height);
System.out.println("width: " + width);
}
int volume(){
return (int)(length*height*width);
}
int surfaceArea(){
return (int) (2*length*height + 2*length*width + 2*width*height);
}
public void setLength(double length){
this.length = length;
}
public void setHeight(double height){
this.length = height;
}
public void setWidth(double width){
this.length = width;
}
public double getLength(){
return length;
}
public double getHeight(){
return height;
}
public double getWidth(){
return width;
}
}
import *;
public class lliuBoxTest {
public static void main(String args[])
throws {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String len, hei, wid;
double valuelength, valueheight,valuewidth;
char choice;
lliuBox x = new lliuBox();
lliuBox y = new lliuBox();
for (;;){
do{
System.out.println("Welcome to create-a-box! What would you like to do?");
System.out.println("1. Create a box");
System.out.println("2. Create a box (dimensions specified)");
do{
choice = (char) System.in.read();
}while(choice == '\n'|choice == '\r');
} while(choice < '1'|choice > '2');
System.out.println();
switch(choice){
case '1':
System.out.println("Selection: ");
System.out.println("1");
System.out.println("A box appeared!");
System.out.println("length: "+ x.getLength());
System.out.println("length: "+ x.getHeight());
System.out.println("length: "+ x.getWidth());
System.out.println("Surface Area: "+ x.surfaceArea());
System.out.println("Volume: "+ x.volume());
break;
case '2':
System.out.println("Selection: ");
System.out.println("2");
System.out.println("Please enter the box's length: " );
len = br.readLine();
valuelength = Double.parseDouble(len);
System.out.println("Please enter the box's height: " );
hei = br.readLine();
valueheight = Double.parseDouble(hei);
System.out.println("Please enter the box's width: " );
wid = br.readLine();
valuewidth = Double.parseDouble(wid);
System.out.println("A box appeared!" );
y.setHeight(valueheight);
y.setLength(valuelength);
y.setWidth(valuewidth);
y.printBox();
System.out.println("Volume: "+ y.surfaceArea());
System.out.println("Surface Area: "+ y.volume());
break;
}
System.out.println();
}
}
}