public class Number {
private int numerator;
private int denominator;
public Number(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public Number() {
this.numerator = 0;
this.denominator = 1;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public String toString() {
if (numerator == 0) {
return "0";
} else if (denominator == 1) {
return denominator + "";
}
return numerator + "/" + denominator;
}
public Number subtract(Number other) {
numerator = (this.getNumerator() * other.getDenominator()) - (other.getNumerator() * this.getDenominator());
denominator = this.getDenominator() * other.getDenominator();
return this;
}
////
public class test {
public static void main(String[] args) {
Number n1 = new Number(1, 2);
Number n2 = new Number(3, 5);
Number n4 = new Number(7, 40);
Number test = n2.subtract(n1);
System.out.println(test);
Number test2 = n4.subtract(n1);
System.out.println(test2);
Number test3 = n1.subtract(n4);
System.out.println(test3);
Number test4 = n2.subtract(n1);
System.out.println(test4);
}
}
我在使用Number subtract()方法时遇到问题。当我从客户端调用它时,它会在某些时候工作,但经过几次连续调用后,我会收到意外的输出。前两个调用工作正常,然后我开始得到结果。我知道我在subtract()方法中遗漏了一些东西,只是不确定它是什么。
任何帮助都将不胜感激。
答案 0 :(得分:2)
如果对对象使用减法方法,请将最后一行更改为return this
。
顺便说一句,你对比率类型的减法是错误的。
答案 1 :(得分:0)
我最终解决了自己的问题,但我感谢每个人的帮助。我知道这可能不是解决这个问题最有效的方法,但我只是CS学生的第一年。如果有人有任何清理此代码的建议,我很乐意听到。谢谢!
public class Number {
private int numerator;
private int denominator;
public Number(int numerator, int denominator) {
reduce(numerator, denominator);
}
public Number() {
this.numerator = 0;
this.denominator = 1;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public String toString() {
if (numerator == 0) {
return "0";
} else if (denominator == 1) {
return numerator + "";
}
return numerator + "/" + denominator;
}
public Number add(RationalNumber other) {
int numerator1 = (numerator * other.getDenominator()) + (other.getNumerator() * denominator);
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number subtract(RationalNumber other) {
int numerator1 = (numerator * other.getDenominator()) - (other.getNumerator() * denominator);
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number multiply(RationalNumber other) {
int numerator1 = numerator * other.getNumerator();
int denominator1 = denominator * other.getDenominator();
return new Number(numerator1, denominator1);
}
public Number divide(RationalNumber other) {
int numerator1 = numerator * other.getDenominator();
int denominator1 = denominator * other.getNumerator();
return new Number(numerator1, denominator1);
}
private void reduce(int numerator, int denominator) {
int factor = 1;
int temp = 0;
if (Math.abs(numerator) > Math.abs(denominator)) {
temp = Math.abs(numerator);
} else if (Math.abs(denominator) > Math.abs(numerator)) {
temp = Math.abs(denominator);
}
for (int i = temp; i > 0; i --) {
if ((numerator %i == 0) && (denominator %i == 0)) {
factor = i;
i = 0;
}
}
numerator = numerator / factor;
denominator = denominator / factor;
rightForm(numerator, denominator);
}
private void rightForm(int numerator, int denominator) {
if (numerator > 0 && denominator < 0) {
this.numerator = numerator - numerator - numerator;
this.denominator = Math.abs(denominator);
} else if (numerator < 0 && denominator < 0) {
this.numerator = Math.abs(numerator);
this.denominator = Math.abs(denominator);
} else {
this.numerator = numerator;
this.denominator = denominator;
}
}
}