0

そのため、宿題として、リンクされた名前のリストを作成し、それを循環させてから、ノードが 1 つになるまで 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