我是学生程序员,所以自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);
}
}
}
答案 0 :(得分: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