如何从对象列表中拆分对象分数?

时间:2016-01-18 05:30:22

标签: java

我之前有一个作业,部分问题包括将一组分数作为字符串数组读取,然后将每个分数分解为分子和分母形式,同时将其转换为整数。所以我用了

for(int i = 0; i < count; i++)
    {
        split = fractions[i].split("/"); 
        //splits the fraction at that point

        //Converts the split string fraction to integer
        numerator[i] = Integer.parseInt(split[0]);
        denominator[i] = Integer.parseInt(split[1]);
}

并且代码如此...... 所以代码运行良好,一切都很好。 现在,我不再使用字符串变量数组,而是要求创建一个名为Objectlist的单独类,其中包含Objects列表,并使用它来拆分分数。 到目前为止,我做了

//Variable for storing numerators
private int numerator;

//variable for storing denominators
private int denominator;

ObjectList fractions = new ObjectList();

//Variable for an array used to split the fraction
private Object[] split = new String[2];

split = (fractions.split("/"); //splits the fraction at that point

    //Converts the split string fraction to integer
    numerator = Integer.parseInt(split[0]);
    denominator = Integer.parse Int(split[1]);

等...... 因此,当我将split变量声明为对象时,我得到3个错误,split在对象列表中未定义,并且使用parse Int进行转换。 当我将split变量声明为字符串时,我得到1个错误,则未在对象列表中定义拆分。

我真的很困惑如何处理这个,因为我很确定我错过了什么。任何帮助都会很棒。

0 个答案:

没有答案