0

我是一名学生程序员,所以从 9 月才开始学习。本周有一项任务是从名为 datingdata.txt 的文本文件中读取数据,并为将每个人的信息存储在数组位置中的信息创建一个类。数据格式为“George Robson M 38 11000”,其中名称最多为 20 个字符。然后我们必须比较每个人的匹配情况,他们是异性,年龄在 5 岁以内,并且至少有 3 个共同兴趣(1 表示他们对该活动感兴趣,0 表示不感兴趣)。我已经让它在最后的嵌套 for 循环中工作,并且感觉我缺少括号,因为它不断出现无法找到符号的错误。有什么帮助吗?

    import java.io.*;
    import static java.lang.Math.*;
    class PersonInfo
   {
    String name;
    char sex;
    int age;
    String interest;

    public void setname (String anyName)
       { name = anyName; }

    public void setsex (char anysex)
       { sex = anysex; }

    public void setage (int anyage)
       { age = anyage; }

    public void setinterest (String anyinterest)
       { interest = anyinterest; }

    public String getName ()
       { return name; }

    public char getsex ()
       { return sex; }

    public int getage ()
    {return age;  }

    public String getinterest ()
    {return interest;    }

    void print ()
       {
        System.out.println(name + " " + sex + " " + age + " " + interest);
       }

    PersonInfo (String name, char sex, int age, String interest)
       {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.interest = interest;
       }
    }

    class Practical6
      {
    public static void main(String[] args) throws IOException
       {
        //read in file
        BufferedReader fileInput;
        fileInput = new BufferedReader(new FileReader("datingdata.txt"));

        //create an array for storing each persons information
        PersonInfo[] People = new PersonInfo[20];

        //fields to use in creating PersonInfo
        String name;
        char sex;
        int age;
        String interest;

        int i = 0;
        int count = 0;

        //read in each line and store in an array
        while (fileInput.ready ())                                  
           {                                                    
            String information;
            information = fileInput.readLine ();                                
           // System.out.println (information);
            name = information.substring(0,20);
            sex = information.charAt(20);
            age = Integer.parseInt(information.substring(22,24));   
            interest = information.substring(25,30);
            People[i]= new PersonInfo (name, sex, age, interest);
            i++;                                
           } 
        fileInput.close(); 

        //pring out the array

        for (int j = 0; j < People.length; j++)
            People[j].print();


        //go through each element of array and compare to another record for a match
        for (int k = 0; k < People.length; k++)
            for (int l = 0; l <People.length; l++)
            if (People[k].getsex() != People[l].getsex())
                if(math.abs(People[k].getage()-People[l].getage()) <=5)
                {for(int m = 0; m < 5; m++)
                    if((People[k].getinterest.charAt(m)=='1')  && (People[l].getinterest.charAt(m)=='1'));
                            count++;
                            if(count==3)
                                System.out.println(People[k].getname + " is a match with " + People[l].getname);
                }

            }
            }
4

1 回答 1

2
for (int k = 0; k < People.length; k++)
    for (int l = 0; l <People.length; l++)
    if (People[k].getsex() != People[l].getsex())
        if(Math.abs(People[k].getage()-People[l].getage()) <=5)
        {for(int m = 0; m < 5; m++)
            if((People[k].getinterest().charAt(m)=='1')  && (People[l].getinterest().charAt(m)=='1'));
                    count++;
                    if(count==3)
                        System.out.println(People[k].getName() + " is a match with " + People[l].getName());
        }

一些语法错误: 1. Math.abs() 而不是 math.abs() 2. getinterest() 而不是 getinterest 3. getName() 而不是 getname

于 2012-11-20T04:24:31.880 回答