标题: 是强转问题吗 [打印本页] 作者: 赵嘉男 时间: 2012-4-22 20:20 标题: 是强转问题吗 public class Pad {
private int a;
private int b;
public Pad(int a,int b) {
this.a = a;
this.b = b;
}
public int sum(){
return a+b;
}
public int sub(){
return a-b;
}
public int mul(){
return a*b;
}
public double div(){
return a/b;
}
public static void main(String[] args) {
Pad h1=new Pad(10,4);
h1.sum();
h1.sub();
h1.mul();
h1.div();
public double div(){
return (double)a/b;
} 这样可以得出2.5
作者: 姚伟涛 时间: 2012-4-22 20:38
package cn.text;
public class Foo {
private int a;
private int b;
public Foo(int a,int b) {
this.a = a;
this.b = b;
}
public int sum(){
return a+b;
}
public int sub(){
return a-b;
}
public int mul(){
return a*b;
}
public double div(){
return (double)a/(double)b;
}
public static void main(String[] args) {
Foo h1=new Foo(10,4);
h1.sum();
h1.sub();
h1.mul();
h1.div();
System.out.println(+h1.sum());
System.out.println(+h1.sub());
System.out.println(+h1.mul());
System.out.println(+h1.div());
}
}
复制代码
传入参数是整形的作者: Friends★ 时间: 2012-4-22 20:42
你要先把变量a、b的int型强转成double或者float型才能输出小数,希望可以帮到你,
public class Pad{
private int a;
private int b;
public Pad (int a,int b) {
this.a = a;
this.b = b;
}
public int sum(){
return a+b;
}
public int sub(){
return a-b;
}
public int mul(){
return a*b;
}
public double div(){
return (double)a/(double)b;
}
public static void main(String[] args) {
Pad h1=new Pad(10,4);
h1.sum();
h1.sub();
h1.mul();
h1.div();