0

因此,对于家庭作业,我必须编写一个程序来创建一个名称链接列表,使其循环,然后删除每个第 n 个节点,直到其中只剩下一件事。我的代码打印出东西被取出但从未真正取出,有人能告诉我为什么吗?注意:因为这是家庭作业,我不是在寻找确切的答案,更多关于如何修复我的代码的有用建议

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Survivor {
    public static void main(String[] args){
        ListNode list=makeNode();
        ListNode t=list;
        Scanner sc=new Scanner(System.in);
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
        System.out.println();
        System.out.print("Enter a number to kill: ");
        int kill=sc.nextInt();
        kill(kill,list);    
        t=list;
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
    }

    public static ListNode makeNode(){
        ListNode names=null;
        File data=new File("names.txt");
        Scanner fl=null;
        ListNode t=names;
        try {
            fl=new Scanner(data);
        } catch (FileNotFoundException e) {
            //handle errors
            System.out.println("The File isnt there"+data.getAbsolutePath()+"\"");
            System.exit(-1);
        }
        while(fl.hasNext()){
            String theName=revName(fl.nextLine());
            if(t!=null){
                if(t.getNext()!=null){
                    ListNode t2=t.getNext();
                    if(theName.compareToIgnoreCase((String) names.getValue())<0){
                        names=(new ListNode(theName, names));                   
                    }
                    if(theName.compareToIgnoreCase((String) names.getValue())>0){               
                        while(t.getNext() !=null &&theName.compareToIgnoreCase((String) t2.getValue())>0 ){                     
                            t=t.getNext();
                            t2=t2.getNext();
                        }
                        t.setNext(new ListNode((theName),t2));
                    }
                }
                if(t.getNext()==null){
                    if(theName.compareToIgnoreCase(((String)t.getValue()))<0){
                        names=(new ListNode((theName),null));
                    }
                    else{
                        t.setNext(new ListNode(theName,null));
                    }
                }
                t=names;
            }
            if(names==null){
                names=new ListNode(theName,null);
                t=names;
            }
            t=names;
        }
        return(names);
    }

    public static String getCauseOfDeath(String name){
        int first=(int)(Math.random()*10);
        String cause = "fell off of a cliff";
        if(first==1)
            cause="tripped";
        if(first==2)
            cause="got a boo-boo and died";
        if(first==3)
            cause="insulted the gods";
        if(first==4)
            cause="had a coconut fall on their head";
        if(first==5)
            cause="drowned";
        if(first==6)
            cause="showed up to cuadrados class late";
        if(first==7)
            cause="tried to steal honey from giant bees and failed";
        if(first==8)
            cause="doesn't even lift";
        if(first==9)
            cause= "got bitten by a unicorn and poisoned";
        if(first==10)
            cause="got lost and starved";
        return("Alas, the mighty " + name+ " has fallen; " + name + " "+cause);
    }

    public static String revName(String name){
        String name1;
        String name2;
        int space;
        int dot;
        dot=name.lastIndexOf(".");
        if (dot == -1){
            name= name.trim(); //Trims spaces at front and end
            space=name.lastIndexOf(" ");

            if (space == -1){  
                //If there are no spaces in the code this line just returns the name
                return name;
            }
            else{
                name1=name.substring(0, space);
                name2=name.substring(space+1);
                if(name2.length()>1){
                    return name2+ " " + name1;
                }
                else{
                    space=name.indexOf(" ");
                    name1=name.substring(0, space);
                    name2=name.substring(space+1);
                    return name2+" "  + name1;
                }
            }
        }
        return (name);
    }

    public static void kill(int i, ListNode list){
        ListNode t=list;
        ListNode t2=t.getNext();
        int z=2;
        while(t2.getNext()!=null){          
            if(i==z){       
                System.out.println(getCauseOfDeath(revName((String) t2.getValue())));
                t.setNext(t2.getNext());
                z=0;
            }
            z++;
            t=t.getNext();
            t2=t2.getNext();
        }
    }
}
4

2 回答 2

0

也许它与将参数传递给 kill 方法有关?

Java 是“按引用传递”还是“按值传递”?

于 2013-06-04T12:25:46.900 回答
0

您可能会考虑使用链表的删除功能来删除元素。另外不要忘记Java确实通过引用来操作对象,并且所有对象变量都是引用。但是,Java 不会通过引用传递方法参数;它按值传递它们。在这里查看有关如何在 Java 中传递事物的更多信息。

于 2013-06-04T12:37:11.397 回答