2016-10-25 11 views
-1

私はリンク先のリストに整数と文字列を続けて追加したいプログラムがあります。しかし、リンクされたリストを印刷するときには、最後に入力された値だけが出力され、前の値は出力されません。だから私が3人のSallyに入って6 Bobに入ったら、リンクされたリストは6 bobだけを出力します。最後に入力したものだけでなく、リンクされたリストのすべてを印刷することができます。リンクリストにデータを繰り返し保存する方法は?

public class Texteditor { 

/** 
* @param args the command line arguments 
*/ 
static int myInt; 
static String myString; 

public Texteditor(int a, String s){ 
    myInt = a; 
    myString = s; 
} 
public String toString(){ 
    return myInt + " " + myString; 
} 
public static void main(String[] args) { 
    LinkedList<Texteditor> myLL = new LinkedList<Texteditor>(); 

    int isExit = 0; 
    System.out.println("Hello Welcome to Your Personal Texteditor! "); 
    System.out.println("There are many options you can do with this text editor"); 
    System.out.println("1. If you enter a line number with no text, the line number will be deleted."); 
    System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number."); 
    System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10."); 
    while(isExit ==0) { 
// myLL = new LinkedList<Texteditor>(); 
    System.out.println(""); 
    System.out.println("Please enter the line number: "); 
    Scanner kb = new Scanner(System.in); 
    myInt = kb.nextInt(); 
    System.out.println("Plese enter text as a string: "); 
    Scanner kb1 = new Scanner(System.in); 
    myString = kb1.nextLine(); 
    Texteditor a1 = new Texteditor(myInt, myString); 
    myLL.add(a1); 
    System.out.println("Would you like to keep going? Enter yes or no: "); 
    Scanner kb2 = new Scanner(System.in); 
    if (kb2.next().equals("no")){ 
     isExit = 1; 
    } 
    } 

    for (Texteditor element : myLL){ 
     System.out.println(element + "\n"); 
    }   
}  
} 

答えて

1

あなたmyIntmyStringは、彼らがインスタンスで共有していることを意味する、静的です。それらを非静的にし、コードが正しく動作するはずです。

また、毎回Scannerをループ内に再作成しないでください。一回だけで十分です。

+0

をありがとうございました! –

0

問題は、myInt変数とmyString変数を静的にしていることです。静的修飾子を削除し、whileループで、クラスのmyIntおよびmyString変数を参照する代わりに、代わりにローカルのintおよびString変数を作成します。

public class Texteditor { 

/** 
* @param args the command line arguments 
*/ 
int myInt; 
String myString; 

    public Texteditor(int a, String s){ 
     myInt = a; 
     myString = s; 
    } 
    public String toString(){ 
     return myInt + " " + myString; 
    } 

    public static void main(String[] args) { 
     LinkedList<Texteditor> myLL = new LinkedList<Texteditor>(); 

     int isExit = 0; 
     System.out.println("Hello Welcome to Your Personal Texteditor! "); 
     System.out.println("There are many options you can do with this text editor"); 
     System.out.println("1. If you enter a line number with no text, the line number will be deleted."); 
     System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number."); 
     System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10."); 
     while(isExit ==0) { 
    // myLL = new LinkedList<Texteditor>(); 
      System.out.println(""); 
      System.out.println("Please enter the line number: "); 
      Scanner kb = new Scanner(System.in); 
      int myInt = kb.nextInt(); 
      System.out.println("Plese enter text as a string: "); 
      Scanner kb1 = new Scanner(System.in); 
      String myString = kb1.nextLine(); 
      Texteditor a1 = new Texteditor(myInt, myString); 
      myLL.add(a1); 
      System.out.println("Would you like to keep going? Enter yes or no: "); 
      Scanner kb2 = new Scanner(System.in); 
      if (kb2.next().equals("no")){ 
       isExit = 1; 
      } 
     } 

     for (Texteditor element : myLL){ 
      System.out.println(element + "\n"); 
     } 

    } 
} 
0

あなたのコードの問題は、変数ミンとのmyStringは静的であり、したがって、彼らは、個々のオブジェクト(彼らはクラスに属している)に属していないということです。したがって、ここでそれらを参照するとき:

for (Texteditor2 element : myLL){ 
       System.out.println(element + "\n"); 
    } 

最後にn回設定したのと同じ値を呼び出しています。

これは、問題を修正する必要があります。

public class TextEditorObject { 
    int myInt; 
    String myString; 

    public TextEditorObject(int a, String s){ 
     myInt = a; 
     myString = s; 
    } 

    public String toString() { 
     return myInt + " " + myString; 
    } 
} 

変更テキストエディタそうのような:

新しいTextEditorObjectファイルを作成

public class Texteditor { 
public static void main(String[] args) { 
    int myInt; 
    String myString; 

    LinkedList<TextEditorObject> myLL = new LinkedList<TextEditorObject>(); 

    int isExit = 0; 
    System.out.println("Hello Welcome to Your Personal Texteditor! "); 
    System.out.println("There are many options you can do with this text editor"); 
    System.out.println("1. If you enter a line number with no text, the line number will be deleted."); 
    System.out.println("2. If you enter LIST alone the editor will print everything in the list with line number."); 
    System.out.println("3. If you enter RESEQUENCE the line numbers will be resequenced to start at 10."); 
    while(isExit ==0) { 
// myLL = new LinkedList<Texteditor>(); 
    System.out.println(""); 
    System.out.println("Please enter the line number: "); 
    Scanner kb = new Scanner(System.in); 
    myInt = kb.nextInt(); 
    System.out.println("Plese enter text as a string: "); 
    Scanner kb1 = new Scanner(System.in); 
    myString = kb1.nextLine(); 
    TextEditorObject a1 = new TextEditorObject(myInt, myString); 
    myLL.add(a1); 
    System.out.println("Would you like to keep going? Enter yes or no: "); 
    Scanner kb2 = new Scanner(System.in); 
    if (kb2.next().equals("no")){ 
     isExit = 1; 
    } 
    } 

    for (TextEditorObject element : myLL){ 
     System.out.println(element + "\n"); 
    } 

} 
} 
関連する問題