关于java的3道题目,请高手,老师进来帮一下

1,public class Test1{ int x,y,z; //分别代表长宽高 public Test1(int x, int y, int z){ this.x = x;this.y = y;this.z = z;} public int jisuang(){ return x*y*z;} public void main(String args[]){ Test1 test = new Test1(1,2,3);//随便输入3个数字 System.ou...
关于java的3道题目,请高手,老师进来帮一下
class StaticError{
static String mystring="hello";
public static void main(String args[]) {
System.out.println(mystring);
}
}2012-10-19
1,
public class Test1{
int x,y,z; //分别代表长宽高
public Test1(int x, int y, int z){
this.x = x;

this.y = y;

this.z = z;

}

public int jisuang(){
return x*y*z;

}

public void main(String args[]){
Test1 test = new Test1(1,2,3);//随便输入3个数字

System.out.println(test.jisuang());

}

}
2,应该是在class前面加public,因为类中有主方法所以该类要声明为公共类。没有编译,应该是这个问题。

3,
public class Test2{
public int test(int x, float y){
return x+y;

}

public int test(){//重载test方法
int x = 1;

float y = 2.0;

return x+(int)y;

}

public void main(Strong args[]){
Test2 test = new Test2();

System.out.println(test.test(2,2.0));

System.out.println(test.test());

}

}

代码是手写的,可能有个别单词会敲错。2012-10-19
1 public class Box {
private double length;
private double width;
private double height;

public Box(double length,double width,double height){
this.length = length;
this.width= width;
this.height=height;
}
public double caculateCubeVal(){
return this.length*this.width*this.height;
}
public static void main(String[] args){
Box box = new Box(12.3d,11d,20d);
double result = box.caculateCubeVal();
System.out.println("box val : "+ result);

}

}
2 String mystring 前 需要声明static
3 public class Example1 {

public int add(int a,int b){
if(a==0) a=10;
if(b==0) b==10;
return a+b;
}
public double add(double a,double b){
if(a==0.0d)a=10.0d;
if(b==0.0d)b=10.0d;
return a+b;
}
}
第三个问题题意不清楚 ,请详述2012-10-19
public class Box {
//长宽高
private int longth ;
private int width;
private int highth;

//构造方法,用来初始化Box
public Box(int longth, int width, int highth) {
super();
this.longth = longth;
this.width = width;
this.highth = highth;
}
//体积计算
public int VOfBox(){
return this.highth*this.longth*this.width;
}

//getter setter 提供属性访问方法
public int getLongth() {
return longth;
}
public void setLongth(int longth) {
this.longth = longth;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHighth() {
return highth;
}
public void setHighth(int highth) {
this.highth = highth;
}

public static void main(String[] args) {
Box b = new Box(1,3,4);
System.out.println("V:"+b.VOfBox());
}
}

2、编译报错,public 方法中只能直接使用Static的变量
3、
public class Test {
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.cal());
System.out.println(t.cal(5, 6f));
}
//重载
//有参数
public float cal(int i ,float f){
return i+f;
}
//无参
public float cal(){
return 0+0f;
}
}2012-10-19
1.
public class Box {
int a;
int b;
int c;
public Box(){
}
public Box(int a,int b,int c){
this.a=a;
this.b=b;
this.c=c;
}

}
public class test {
public int BoxV(Box box){
int v;
v=box.a*box.b*box.c;
return v;
}
public static void main(String args[]) {
Box box=new Box(1,2,3);
System.out.println(new test().BoxV(box));

}

}

2.
class StaticError{
static String mystring="hello";
public static void main(String args[]) {
System.out.println(mystring);
}
}
或者
class StaticError{
public static void main(String args[]) {
String mystring="hello";
System.out.println(mystring);
}
}
3 .
public class test {
public float add(){
float c=0;
c=1+2;
return c;
}
public float add(int a, float b){
float c=0;
c=a+b;
return c;
}
public double add(int a, double b){
double c=0;
c=a+b;
return c;
}

public static void main(String args[]) {
System.out.println(new test().add());
System.out.println(new test().add(1,1));
System.out.println(new test().add(1,2.1));

}

}2012-10-19
1.你是想让别人帮忙编程?你要是得到了长宽高,那么直接传入求体积的方法中,相乘就行了,然后将结果返回到你要的地方。
2.静态方法(static)中不能调用非静态变量。
3.找本书,自己看,不用多看,看前几章就行了。2012-10-19
1.
public class Box {
private double length;
private double width;
private double height;

public Box(double length, double width, double height) {
super();
this.length = length;
this.width = width;
this.height = height;
}

public double getV(){
return this.length*this.width*this.height;
}

public static void main(String[] args) {
Box box=new Box(3, 2, 4);
System.out.println(box.getV());
}

}

2.
引号错误,是英文输入下的引号。
3
class Add{

private int a;
private int b;

public int add(int a,int b){
return a+b;
}

public float add(float a,float b){
return a+b;
}

public int add(){
return a+b;
}
}2012-10-19
第二个,main函数中代码改成:
StaticError se = new StaticError();
System.out.println(se.mystring);2012-10-19
路过2012-10-19
mengvlog 阅读 9 次 更新于 2025-07-19 13:13:56 我来答关注问题0
  • public void main(String args[]){ Test1 test = new Test1(1,2,3);//随便输入3个数字 System.out.println(test.jisuang());} } 2,应该是在class前面加public,因为类中有主方法所以该类要声明为公共类。没有编译,应该是这个问题。3,public class Test2{ public int test(int x, float ...

  •  citytalent 请JAVA高手帮我编3道小题 高分。。

    import java.util.Scanner;public class Test99 { public static void main(String[] args) { System.out.println("请输入一个数字");Scanner sc = new Scanner(System.in);String s = sc.nextLine();// 获取输入的字符串 int i = Integer.parseInt(s);//将字符串转化为数字 System.out.pri...

  •  HanJdEx 几道JAVA题目,求好心人给下答案,感激不尽

    1、public static void main(String[] args)2、public finall static 3、3 4、抽象类 5、implements 6、类:一个包含属性、方法,使用class关键字定义 对象:创建对象必须使用new关键字,对象其实在内存中存储的是类的引用地址。7、try是可能发生异常的内容;catch是发生异常后要处理的代码;finally是...

  •  百度网友66d722a 三道java的选择题,麻烦大家了。

    3>这题也好理解,concat(str)函数的意思是将传入的str拼接到当前字符串的末尾,但是注意composite.concat("world")之后并未把结果重新赋值给composite,所以composite的值还是执行这个语句之前的值,然后执行trim()方法之后,把composite字符串的所有空格去掉,在赋值给trimmed。结果就是hello.所以字符串的长度...

  •  497123505 3道JAVA 小程序题,求高手解答!!

    import java.util.Scanner;public class Du { public static void main(String[] args) throws Exception { first();} private static void first() throws Exception {//第一道题 System.out.print("Please input a digt between 0 and 9: ");int input = new Scanner(System.in).nextInt(...

檬味博客在线解答立即免费咨询

Java相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部