| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1469 人关注过本帖
标题:新手java作业,创建一个box输入长宽高求面积体积
只看楼主 加入收藏
llawlh
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-2-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
新手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();
                }
        
}
}
搜索更多相关主题的帖子: java class 不胜感激 private public 
2012-02-17 05:05
mayuebo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:6
帖 子:257
专家分:1282
注 册:2005-9-8
收藏
得分:7 
String len, hei, wid;
直接定义成float吧,然后用从输入流中获取

成功贵在坚持
2012-02-17 07:43
懂也不懂
Rank: 4
等 级:业余侠客
威 望:2
帖 子:73
专家分:224
注 册:2010-12-1
收藏
得分:7 
你的程序似乎会在进入2功能后异常,在len = br.readLine();之前再加一句br.readLine()来过滤上次输入不可用字符就能运行了
2012-02-17 10:33
小草草编程
Rank: 1
等 级:新手上路
帖 子:3
专家分:7
注 册:2012-2-20
收藏
得分:7 
public void setLength(double length){
    this.length = length;
}
public void setHeight(double height){
    this.length = height;
}
public void setWidth(double width){
    this.length = width;
}
我想你这段程序有问题吧?你准备给height和width赋值,应该是:
public void setHeight(double height){
   this.height=height;
}
public void setWidth(double width){
   this.width=width;
}想必是大意了吧,呵呵。
2012-02-20 17:17
快速回复:新手java作业,创建一个box输入长宽高求面积体积
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019219 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved