| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1896 人关注过本帖
标题:求大神帮忙指出一下哪里错了!!!
只看楼主 加入收藏
小白瘤
Rank: 1
来 自:江西
等 级:新手上路
帖 子:10
专家分:0
注 册:2018-7-28
结帖率:25%
收藏
已结贴  问题点数:10 回复次数:4 
求大神帮忙指出一下哪里错了!!!
我是初学者,正在学java,但是我按着书上的代码敲,结果它还是会报错,什么缺少;什么需要标识符的。我找了好久还是没找到,已经不知道是什么原因了。
这是程序代码
程序代码:
class Box{
    double width;
    double height;
    double depth;
    
    double volume(){
        return width*height*depth;
    }
    
    void setDim(double w, double h, double d){
        width=w;
        height=h;
        depht=d;
    }
}

class BoxTest{
    public static void main(String[] s)
    Box myBox=new Box();
    Box hisBox=new Box();
    double myVol,hisVol;
    myBox.setDim(10, 20, 15);
    hisBox.setDim(3, 6, 9);
    myVol=myBox.volume();
    hisVol=hisBox.volume();
    System.out.println("myBox is"+myBox);
    System.out.println("hisBox is"+hisBox);
}

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: class Box double void new 
2018-10-20 09:47
红柚
Rank: 4
来 自:咸鱼乡的酸菜鱼
等 级:贵宾
威 望:14
帖 子:53
专家分:218
注 册:2018-4-20
收藏
得分:3 
程序代码:
class Box{
    double width;
    double height;
    double depth;
    
    double volume(){
        return width * height * depth;
    }
    
    void setDim(double w, double h, double d){
        width = w;
        height = h;
        depth = d;    //这里你打成depht了
    }
}

public class BoxTest{
    public static void main(String []args){
        Box myBox = new Box();
        Box hisBox = new Box();
        double myVol, hisVol;
        myBox.setDim(10, 20, 15);
        hisBox.setDim(3, 6, 9);
        myVol = myBox.volume();
        hisVol = hisBox.volume();
        System.out.println("myBox is " + myVol);
        System.out.println("hisBox is " + hisVol); //看起来你想要的应该是输出hisVol,但是你的是hisBox,上一行也是这样
    }
}


程序代码:
public class BoxTest {
    public static void main(String[] args) { ... }
}


现在先记得这个开头吧,main在的类要加public的,后面别忘了要用花括号括起来

[此贴子已经被作者于2018-10-20 13:09编辑过]

2018-10-20 12:57
wlrjgzs
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:26
帖 子:212
专家分:1566
注 册:2017-4-10
收藏
得分:3 
2楼正解。不过根据面向对象的封装原则,你那个Box应该修改一下,以符合面向对象的封装原则,代码如下:
程序代码:
class Box{
    private double width;     //这3个成员的访问权限设置为private,不能在类外直接访问,只能通过下面方法进行访问。确保这3个私有成员不被意外赋值。
    private double height;
    private double depth;
    
    double volume(){
        return this.width * this.height * this.depth;
    }
    
    void setDim(double w, double h, double d){
        this.width = w;
        this.height = h;
        this.depth = d; 
    }
}
2018-10-20 16:14
新人学习
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:145
专家分:187
注 册:2017-10-26
收藏
得分:3 
在包含main() 方法 的类前面要加上public 关键字
main() 方法没有用花括号.
2018-10-25 17:03
陈无
Rank: 2
等 级:论坛游民
帖 子:29
专家分:35
注 册:2018-10-10
收藏
得分:3 
   加一个public试试,
2018-10-25 23:06
快速回复:求大神帮忙指出一下哪里错了!!!
数据加载中...
 
   



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

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