System.out.println(f1.value() + "\t// Correct answer: -0.75");
你的jdk版本用8,编译器版本用2019年以后的,别用老的。
f1 是如何定义和赋值的? 请把完整代码写出来。
public class TestFraction {
public static void main(String[] args) {
Fraction f1, f2;
f1 = new Fraction(3, 0);
System.out.println(f1 + "\t// Correct answer: 1/2");
f1 = new Fraction(0, -4);
System.out.println(f1 + "\t// Correct answer: 0/1");
f1 = new Fraction(0, 0);
System.out.println(f1 + "\t// Correct answer: 0/1");
f1 = new Fraction(3, 4);
System.out.println(f1 + "\t// Correct answer: 3/4");
f1 = new Fraction(3, -4);
System.out.println(f1 + "\t// Correct answer: -3/4");
System.out.println(f1.value() + "\t// Correct answer: -0.75");
f1 = new Fraction(-6, -4);
System.out.println(f1 + "\t// Correct answer: 3/2");
f2 = new Fraction(1, 2);
System.out.println(f1.value() - f2.value() + "\t// Correct answer: 1.0");
}
}
这个value 是 功能来的吗? 怎么可以把分数换成double ?
Fraction是实体类,你的value是double类型的吗?
是的,但是应该怎么写?
public double value() {
毫无头绪
return 0;
这是一个对象调用方法的过程,value是一个方法,你想把它做成一个分数转换的方法:
public double value(Integer score){
// 整数转换成字符串,再转换成double
double v = Double.parseDouble(String.valueOf(score));
return v;
}
The method value(Integer) in the type Fraction is not applicable for the arguments
还是不行