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