Topic: Java小问题

  Print this page

1.Java小问题 Copy to clipboard
Posted by: Googlefeifei
Posted on: 2007-04-18 01:02

class add
{
  static double add1(double x,double y)
  {
    double z,a=0;
    z=x+y;
    System.out.println("z="+z);
    x=x+3.2;y=y+1.2;
    System.out.println("x="+x+"\ty="+y);
    return a;
  }
  static double add2(double y1,double y2)
  {
    double z;
    z=y1+y2+2.9;
    return z;
    
  }
}

public class c4_12
{
  public static void main(String[] args)
  {
    add A=new add();
    int a=2,b=7;
    double f1=2,f2=4,f3;
    System.out.println(A.add1(a,b));
    System.out.println("a="+a+"\tb="+b);
    f3=2+A.add2(f1,f2);
    System.out.println("f1="+f1+"\tf2="+f2+"\tf3="+f3);
    
  }
}

输出结果:
--------------------Configuration: <Default>--------------------
z=9.0
x=5.2 y=8.2
0.0
a=2 b=7
f1=2.0 f2=4.0 f3=10.9

Process completed.

我不想要出现0.0这个数字
即,我想要的效果是:
z=9.0
x=5.2 y=8.2
a=2 b=7
f1=2.0 f2=4.0 f3=10.9

哪位大虾,给小弟指导一下,万分感谢!!

2.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: denghouming
Posted on: 2007-04-18 22:19

你的要求改好了,函数Println调用必须输出有返回值的,我这改一下可以实现你要求了,你好好想
public class add
{
static double add1(double x,double y)
{
double z,a=0;

z=x+y;
System.out.println("z="+z);
x=x+3.2;y=y+1.2;
System.out.println("x="+x+"\ty="+y);
return z ;

}
static double add2(double y1,double y2)
{
double z;
z=y1+y2+2.9;
return z;

}

public static void main(String[] args)
{
add A=new add();/*主要是这里*/
int a=2,b=7;
double f1=2,f2=4,f3;
A.add1(a,b);
System.out.println("a="+a+"\tb="+b);
f3=2+A.add2(f1,f2);
System.out.println("f1="+f1+"\tf2="+f2+"\tf3="+f3);

}
}
想一下吧!!呵呵.........

3.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: Googlefeifei
Posted on: 2007-04-18 23:06

噢噢,我忘了,还有个约束条件喔!!

就是,要求有两个类喔!!
不过,还是谢过上面那位啦!!

4.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: aaarong
Posted on: 2007-04-18 23:30

class add
{
static double add1(double x,double y)
{
double z,a=0;
z=x+y;
System.out.println("z="+z);
x=x+3.2;y=y+1.2;
System.out.println("x="+x+"\ty="+y);
return a; //返回的是a 的值 a 为double 型
}
static double add2(double y1,double y2)
{
double z;
z=y1+y2+2.9;
return z;

}
}

public class c4_12
{
public static void main(String[] args)
{
add A=new add();
int a=2,b=7;
double f1=2,f2=4,f3;
A.add1(a,b); //同denghouming 改的一样.
// System.out.println(A.add1(a,b));// 所以这句输出才会输出a 的值呀.
System.out.println("a="+a+"\tb="+b);
f3=2+A.add2(f1,f2);
System.out.println("f1="+f1+"\tf2="+f2+"\tf3="+f3);

}
}

5.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: Googlefeifei
Posted on: 2007-04-19 00:22

高,实在是高!!
将 System.out.println(A.add1(a,b));屏蔽就行了!!

没看你的回帖之前我后来做的,跟你的对比一下:

class add
{
  static void add1(double x,double y)
  {
    double z;
    z=x+y;
    System.out.println("z="+z);
    x=x+3.2;y=y+1.2;
    System.out.println("x="+x+"\ty="+y);
  
  }
  static double add2(double y1,double y2)
  {
    double z;
    z=y1+y2+2.9;
    return z;
    
  }
}

public class c4_12
{
  public static void main(String[] args)
  {
    add A=new add();
    int a=2,b=7;
    double f1=2,f2=4,f3;
    A.add1(a,b);
    System.out.println("a="+a+"\tb="+b);
    f3=2+A.add2(f1,f2);
    System.out.println("f1="+f1+"\tf2="+f2+"\tf3="+f3);
    
  }
}

/*输出结果:
--------------------Configuration: <Default>--------------------
z=9.0
x=5.2 y=8.2
a=2 b=7
f1=2.0 f2=4.0 f3=10.9

Process completed.*/

呵呵,两个都得到了想要的结果!!

上楼的回答,z值没有显示出来,应该是只有返回值,没有显示函数来显示吧!!

谢谢大家指点!!我会更加努力学习Java的!!

6.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: aaarong
Posted on: 2007-04-20 08:40

对 若有return 语句 就有返回值

7.Re:Java小问题 [Re: Googlefeifei] Copy to clipboard
Posted by: 112233445566
Posted on: 2007-04-20 23:09

学习了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923