我是一名新手程序员。我正在尝试创建一个接受String的类,返回一个有理数,我打算用这个类进行基本的数学计算(+ - * /)。我将我的字段设置为public并添加了一个接受字符串的构造函数,然后测试一个数字是否为十进制数,如果是十进制则将其转换为rational,如果不是则将其转换为相同。但是,当我尝试编译时,我收到此错误:“类,接口或枚举预期”我检查我的{}并且它们似乎都已到位..这是我的代码
import java.lang.*;
public class RationalNumbers{
double numarator;
double denominator;
public RationalNumbers (String(val)){
numerator = Integer.parseInt(val);
for (int i = 1; i< val.length(); i++){
if(Val.charAt(i) == '.'){
numerator = Math.round((numerator * Math.pow(10,(numeratorAsString.length() - (i+ 1)))));
denominator = Math.pow(10,(numeratorAsString.length() - (i+ 1)));
}else {
numerator = numarator * 1;
denominator = 1;
}
}
}
public String toString(){
return "(" + numarator + " " + denominator + ")";
}
}
答案 0 :(得分:5)
将public RationalNumbers (String(val)){
更改为
public RationalNumbers (String val){
答案 1 :(得分:1)
/**
* extends the functionality of the Date() object to include a function called addDays that adds days to
* a javascript date based on an integer
* @param days
* @returns {Date}
*/
Date.prototype.addDays = function(days){
var dat = new Date(this.valueOf());
dat.setDate(dat.getDate() + days);
return dat;
};
应该声明为
public RationalNumbers (String(val)){
答案 2 :(得分:1)
String(val)
请勿在{{1}}中包装变量名称,只需说()