从阵列列表中打印特定部件

时间:2017-05-30 22:08:20

标签: java arrays

所以我有一个Arraylist,它持有一个类,如下面的代码所示。

NewStorage.java是包含以下代码的文件的名称

package postit;

import java.util.ArrayList;
import java.util.Scanner;

public class NewStorage extends NoteInfomation {

    Scanner inputID = new Scanner(System.in);
    Scanner NoteID = new Scanner(System.in);

    int count = 0;

    ArrayList<NoteInfomation> Note = new ArrayList<NoteInfomation>(20);

    public void printinfo() {
        System.out.println("--- Fill note here  ---");
    }

    public void Notestore() {

        System.out.println("Enter the note ID you wish to attach the note with\n\n");
        inputIDnote = inputID.nextLine();
        System.out.println("Enter your note\n\n");
        noteDescription = NoteID.nextLine();
        System.out.println("" + inputIDnote);
        System.out.println("" + noteDescription);
    }

    public void PrintNotes() {

        for (int i = 0; i < Note.size(); i++) {
            System.out.println(" " + Note.get(i));

        }
    }
}

这是名为Postit.java的主文件,如下所示

package postit;

import java.util.Scanner;

public class Postit {

    public static void main(String[] args) {
        int MenuOption = 0;

        NewStorage G = new NewStorage();    // Case 1 Object

        while (MenuOption != 3) {

            System.out.println(
                    "\n--------Note System-------\n" +
                            "----------------------------\n" +
                            "1.   Create a Note \n" +
                            "2.   View Notes \n" +
                            "3.   Close Program\n" +
                            "----------------------------\n"
            );

            Scanner menu = new Scanner(System.in);
            MenuOption = menu.nextInt();

            switch (MenuOption) {
                case 1:
                    G.printinfo();
                    G.Notestore();
                    break;
                case 2:
                    G.PrintNotes();
                    break;
                case 3:
                    System.out.println("Program is closing");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice.");
                    break;
            }
        }
    }
}

最后这是note.java文件,其中包含用于数组的NoteInfomation类。

package postit;

class NoteInfomation {
    String inputIDnote;
    String noteDescription;
}

所以,我的问题是,如代码所示,程序应该询问用户他们想要分配他们的笔记的笔记ID。然后,用户可以通过输入相应的音符ID来查看他们创建的音符。理想情况下,当我在程序的基础上构建时,我也希望程序允许用户从以前创建的ID列表中进行选择,以记住创建的笔记的ID不会成为问题。提前致谢。

1 个答案:

答案 0 :(得分:1)

您的代码中存在大量错误。您没有在Notestore()方法中存储任何记事。 PrintNode()也无效。

将您的代码更改为类似的内容..

<强> NoteInfomation.java

class NoteInfomation {
    String inputIDnote;
    String noteDescription;
    public NoteInfomation(String inputIDnote, String noteDescription) {
        this.inputIDnote = inputIDnote;
        this.noteDescription = noteDescription;
    }
    @Override
    public String toString() {
        return "Id:" + inputIDnote + ", Description: " + noteDescription;
    }
}

Nodestore()方法......

public void Notestore() {

    System.out.println("Enter the note ID you wish to attach the note with\n\n");
    String inputIDnote = inputID.nextLine();
    System.out.println("Enter your note\n\n");
    String noteDescription = inputID.nextLine();  //No need for two scanner
    System.out.println("" + inputIDnote + "\n" + noteDescription);
    Note.add(new NoteInfomation(inputIDnote, noteDescription));
}

PrintNotes()方法

public void PrintNotes() {

    for (int i = 0; i < Note.size(); i++) {
        System.out.println(" " + Note.get(i).toString());

        //**or you can also use this
        //  System.out.println("Id:" + Note.get(i).inputIDnote + ", Description: " + Note.get(i).noteDescription);

    }
}

我鼓励你研究ArrayList的工作原理。