Java 递归混淆
我的程序从外部文本文件中获取数据,并将这些数据放入名称和数字列表中。这些数字代表友谊(即,如果在索引2中,一个人有一个1。这意味着这个人与名字为[1]的人是朋友)。从这里,它应该取一个名字,显示这个人的直接朋友是谁,然后显示间接朋友(间接朋友是一个人朋友的朋友)我当前的问题是,我的程序显示间接的,但我不确定如何将其发送到我的base if语句中以打印直接的朋友。如果有人能透露一些情况,我们将不胜感激 名称数组中的数据(字符串):[“Smith”、“Adams”、“Steward”、“Stein”、“Mankell”、“Yorst”] 数字数组中的数据(字符串): [“000010”、“001001”、“01000”、“000010”、“100100”、“010000”] 我在smith上调用了这个方法(directFriends(“smith”),名字,数字,1) 我把这个拿出来: 史密斯的间接朋友: 曼克尔是史密斯的朋友 曼克尔是斯坦的朋友Java 递归混淆,java,recursion,Java,Recursion,我的程序从外部文本文件中获取数据,并将这些数据放入名称和数字列表中。这些数字代表友谊(即,如果在索引2中,一个人有一个1。这意味着这个人与名字为[1]的人是朋友)。从这里,它应该取一个名字,显示这个人的直接朋友是谁,然后显示间接朋友(间接朋友是一个人朋友的朋友)我当前的问题是,我的程序显示间接的,但我不确定如何将其发送到我的base if语句中以打印直接的朋友。如果有人能透露一些情况,我们将不胜感激 名称数组中的数据(字符串):[“Smith”、“Adams”、“Steward”、“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);
}
}
}
}
}
}
}
我期望的输出是:
史密斯是曼克尔的朋友
史密斯的间接朋友:
曼克尔是史密斯的朋友
曼克尔是斯坦的朋友
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);
}
}
}
}
}
}
}
公共类所有好友{
公共静态void directFriends(字符串名称、字符串[]名称、字符串[]编号、int num){
ArrayList indirect=新的ArrayList();
如果(num==0){
对于(int i=0;i我实际上让它为任何好奇的人工作了这里是我的解决方案我所要做的就是重新排序,并在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);**
}
}
}
}
}
}
}
公共类所有好友{
公共静态void directFriends(字符串名称、字符串[]名称、字符串[]编号、int num){
ArrayList indirect=新的ArrayList();
如果(num==0){
for(int i=0;iQuestions寻求调试帮助(“此代码为什么不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现所需的最短代码。没有明确问题说明的问题对其他读者没有用。“包括所需的行为”:这意味着您需要提供正在使用的数据和您期望的实际输出。@ScottHunter感谢您对此进行澄清。很抱歉误解。我刚刚用以下信息更新了问题:史密斯(或其他人)如何才能当斯坦因不在名单上时,直接或以其他方式与他交朋友?@ScottHunter这实际上是我的错误,我刚刚把他加进去了。但我也成功了谢谢你的澄清