我的程序从外部文本文件中获取数据,并将此数据放入名称和数字列表中。这些数字代表友谊(即如果在索引2处有一个人为1。这意味着这个人是名字[1]的人的朋友。)从这里起,它应该取一个名字并显示该人和# 39;直接的朋友然后显示间接的朋友(间接朋友是一个人的朋友。)我当前的问题是我的程序显示间接但我不知道如何将它发送到我的基地如果声明打印直接的朋友。如果有人能够得到一些非常感激的亮点。
名字数组中的数据(字符串):[" Smith"," Adams"," Steward"," Stein", "曼凯尔"" Yorst"]
数字数组(String)中的数据: [" 000010"" 001001"" 01000"" 000010"" 100100"" 010000"]
我在史密斯上调用了这个方法(directFriends(" Smith",姓名,数字,1)
我正在接受这个:
间接史密斯之友:
Mankell是Smith的朋友
Mankell是Stein的朋友
我想要的输出是:
史密斯是Mankell的朋友
间接史密斯之友:
Mankell是Smith的朋友
Mankell是Stein的朋友
public class AllFriends {
public static void directFriends(String name,String[]names,String[]numbers, int num){
ArrayList<String> indirect = new ArrayList<String>();
if (num==0){
for (int i=0;i<names.length;i++){
if(names[i].equalsIgnoreCase(name)){
for(int j=0;j<numbers[i].length();j++){
if(numbers[i].charAt(j)=='1'){
System.out.println(name+" is friends with "+names[j]);
}
}
}
}
}
else
//Go through names array
for (int i=0;i<names.length;i++){
//To see if name entered is the same as a name from the list
if(names[i].equalsIgnoreCase(name)){
//For each character in the individual number String
for(int j=0;j<numbers[i].length();j++){
//Check if it's a 1(Friends)
if(numbers[i].charAt(j)=='1'){
//Then add this friend to the group of people needed to be friend searched
indirect.add(names[j]);
//If
if(j==numbers[i].lastIndexOf("1") ) {
for(int f=0;f<indirect.size();f++){
System.out.println("Indirect Friends of "+name+":");
num--;
directFriends(indirect.get(f),names,numbers,num);
}
}
}
}
}
}
}
答案 0 :(得分:0)
我实际上让它适用于任何对此感到好奇的人都是我的解决方案所有我必须要做的就是重新订购并在else语句中在我的最终for循环中添加一行:
public class AllFriends {
public static void directFriends(String name,String[]names,String[]numbers, int num){
ArrayList<String> indirect = new ArrayList<String>();
if (num==0){
for (int i=0;i<names.length;i++){
if(names[i].equalsIgnoreCase(name)){
for(int j=0;j<numbers[i].length();j++){
if(numbers[i].charAt(j)=='1'){
System.out.println(name+" is friends with "+names[j]);
}
}
}
}
}
else
//Go through names array
for (int i=0;i<names.length;i++){
//To see if name entered is the same as a name from the list
if(names[i].equalsIgnoreCase(name)){
//For each character in the individual number String
for(int j=0;j<numbers[i].length();j++){
//Check if it's a 1(Friends)
if(numbers[i].charAt(j)=='1'){
//Then add this friend to the group of people needed to be friend searched
indirect.add(names[j]);//New stuff
//If
if(j==numbers[i].lastIndexOf("1") ) {
for(int f=0;f<indirect.size();f++){
num--;
**directFriends(name,names,numbers,num);
System.out.println("Indirect Friends of "+name+":");
directFriends(indirect.get(f),names,numbers,num);**
}
}
}
}
}
}
}