请问这个java程序怎么写的?
答案:2 悬赏:70 手机版
解决时间 2021-01-28 19:28
- 提问者网友:临风不自傲
- 2021-01-28 04:44
题目:创建一个类,它含有实例变量height,weight,depth。变量类型都是int型。创建一个java程序,它使用这个新类,并给对象的这些实例变量赋值,然后显示这些值。
最佳答案
- 五星知识达人网友:洒脱疯子
- 2021-01-28 05:28
public class Cube {
private int hieght;
private int weight;
private int depth;
public Cube() {
super();
// TODO Auto-generated constructor stub
}
public Cube(int hieght, int width, int depth) {
super();
this.hieght = hieght;
this.weight = width;
this.depth = depth;
}
public int getHieght() {
return hieght;
}
public void setHieght(int hieght) {
this.hieght = hieght;
}
public int getWeight() {
return weight;
}
public void setWeigth(int weigth) {
this.weight = weigth;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
@Override
public String toString() {
return "Cube [hieght=" + hieght + ", width=" + weight + ", depth="
+ depth + "]";
}
public static void main(String[] args) {
Cube cube = new Cube(1,2,3);
System.out.println(cube.toString());
}
}初始化为1,2,3,。显示结果:
Cube [hieght=1, width=2, depth=3]
private int hieght;
private int weight;
private int depth;
public Cube() {
super();
// TODO Auto-generated constructor stub
}
public Cube(int hieght, int width, int depth) {
super();
this.hieght = hieght;
this.weight = width;
this.depth = depth;
}
public int getHieght() {
return hieght;
}
public void setHieght(int hieght) {
this.hieght = hieght;
}
public int getWeight() {
return weight;
}
public void setWeigth(int weigth) {
this.weight = weigth;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
@Override
public String toString() {
return "Cube [hieght=" + hieght + ", width=" + weight + ", depth="
+ depth + "]";
}
public static void main(String[] args) {
Cube cube = new Cube(1,2,3);
System.out.println(cube.toString());
}
}初始化为1,2,3,。显示结果:
Cube [hieght=1, width=2, depth=3]
全部回答
- 1楼网友:不甚了了
- 2021-01-28 06:31
import java.util.inputmismatchexception; import java.util.scanner; public class numinput { private int sum; //输出累计结果 private void numin(){ scanner num = new scanner(system.in); try{ int temp = num.nextint(); if(temp>0){ sum =temp; system.out.println("累计结果:" sum); numin();//递归循环输入 }else{ system.out.println("结束"); } }catch(inputmismatchexception e){ system.out.println("请输入数字"); numin();//递归循环输入 } return; } public static void main(string[] args) { numinput test = new numinput(); test.numin(); } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯