gpt4 book ai didi

java - 比较数组元素的代码出错

转载 作者:行者123 更新时间:2023-12-02 07:26:56 24 4
gpt4 key购买 nike

我是一名学生程序员,所以从九月份才开始学习。本周有一项作业,从名为约会data.txt 的文本文件中读取数据,并为将每个人的信息存储在数组位置中的信息创建一个类。数据的格式为“George Robson M 38 11000”,其中名称最多 20 个字符。然后我们必须比较每个人的匹配情况,他们是异性,年龄相差不超过 5 岁,并且至少有 3 个共同兴趣(1 表示他们对该 Activity 感兴趣,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);
}

}
}

最佳答案

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()代替getinterest3. getName()代替getname

关于java - 比较数组元素的代码出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13459933/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com