如何解决如何修复 <Identifier> 预期错误?
我正在编写一个函数,该函数将另一个类中构造的对象用作变量。 这是代码,我在这里得到错误“commonDenominator(Fraction,Fraction)”在分数和分数中预期的标识符的两个错误。
public class MyMath {
public static int commonDenominator(Fraction,Fraction){
int d1 = getDenominator();
int d2 = getDenominator();
if (d1%d2==0){
return d1;
} else if (d2%d1=0){
return d2;
} else {
return d1*d2 ;
}
}
}
另一方面,这是分数类的代码:
public class Fraction {
// The fields of this Fraction
private int numerator;
private int denominator;
/** Constructs a fraction.
* The newly constructed fraction is reduced.
* For example,given 6 and 9,constructs the fraction 2/3.
* If the denominator is negative,converts the signs of both the numerator and the denominator.
* For example,2/-3 becomes -2/3,and -2/-3 becomes 2/3.
* @param numerator can be signed
* @param denominator can be signed
*/
public Fraction (int numerator,int denominator) {
// Handles the signs of the numerator and denominator
if (denominator < 0) {
numerator = numerator * -1;
denominator = denominator * -1;
}
// Initializes the object's fields
this.numerator = numerator;
this.denominator = denominator;
// Divides the numerator and denominator by their greatest common divisor
}
/** Constructs a random fraction.
* The denominator is a random positive random number which is less than limit,and
* the numerator is a random positive random number which is less than the denominator.
* @param limit must be greater or equal to 1
*/
public Fraction(int limit) {
// Put your code here
int denominator1 = (int)(Math.random()*(limit+1));
int numerator1 = (int)(Math.random()*(denominator + 1));
this.numerator = numerator1;
this.denominator = denominator1;
}
/** Returns the numerator of this fraction.
* @return the numerator of this fraction
*/
public int getNumerator() {
return numerator;
}
/** Returns the denominator of this fraction.
* @return the denominator of this fraction
*/
public int getDenominator() {
return denominator;
}
}
解决方法
您需要为方法的参数命名。可以推测,d1
和 d2
应该分别是它们的分母:
public static int commonDenominator(Fraction f1,Fraction f2) {
// Here ----------------------------------^------------^
int d1 = f1.getDenominator(); // Use f1
int d2 = f2.getDenominator(); // Use f2
if (d1%d2==0){
return d1;
} else if (d2%d1=0){
return d2;
} else {
return d1*d2 ;
}
}
,
public static int commonDenominator(Fraction,Fraction){
问题是这个方法声明不完整。在 Java 中,参数必须具有类型(在本例中为 Fraction
)和名称。您的参数缺少名称。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。