0

因此,我正在开发一个名为 Notes 计数器的程序。在第二个 java 文件中,我想要的是询问用户,他/她想要添加多少注释,然后按顺序显示所有注释(1.2.....)

我无法将多个 JOoptionPane.showInpuDialogs 放入一个数组中 – user2547460 31 秒前编辑

对于这一行:

for(int i = 0; userEnterADD >i;i++){
String add1 = JOptionPane.showInputDialog("Enter your note here!");
 numberNotes= new String[userEnterADD];}

abobe 方法应将来自 JOoptioPane 的所有用户答案放入一个数组中。所以稍后我可以将用户为 JOOptionPane 输入的所有注释打印为一个数组,

第二个文件查看器N:

所以我想问用户,“你想添加多少条笔记”?我将此字符串存储为int。然后我想问用户“输入你的笔记”的次数与 int 一样多(你想添加多少笔记?)。

然后我想将用户的答案存储在一个数组中。字符串 numberNotes[] 数组,并在 infoView() 中打印出来。希望你能理解这一点!!谢谢

我想将用户在此处输入的注释打印为一个数组,我该怎么做?

感谢 public void infoView(){

System.out.println("\n\tYour notes:\n");
for(int ii = 0; userEnterADD >ii;ii++){
        System.out.println(ii+1 + ". " + numberNotes[ii]);

    //end for
    }
    }



    // end of the program
}
4

2 回答 2

0
for(int i = 0; userEnterADD >i;i++){
     String add1 = JOptionPane.showInputDialog("Enter your note here!");
     numberNotes= new String[userEnterADD];
}

此代码将始终覆盖您的数组。

要将值添加到数组,请按如下方式使用:

for(int i = 0; userEnterADD >i;i++){
     String add1 = JOptionPane.showInputDialog("Enter your note here!");
     numberNotes[i] = add1;
}

旁注:下次避免在您的帖子中出现这种混乱。75% 的代码几乎不相关,您可以将其省略,这将使每个人都更容易。学习如何自己识别问题很重要,通过确保此处的问题包含所有信息,您通常会发现问题。

于 2013-10-08T07:15:29.823 回答
0

您需要进行以下更改

userEnterADD = Integer.parseInt(numbAddn);
numberNotes = new String[userEnterADD]; // Need to initialize your numberNotes array here.
...
for (int i = 0; userEnterADD > i; i++) {
    String add1 = JOptionPane.showInputDialog("Enter your note here!");
    numberNotes[i] = add1; // Add the text received from the user to your array
}
..
// System.out.println(numberNotes[2]); // Commen this line

你所做的将继续用一个新的字符串数组覆盖你当前的数组。该 SOP 需要评论,因为

  1. 它没有任何目的
  2. ArrayIndexOutOfBoundsException如果用户想要输入 2 个或更少的音符,它会给出一个。

编辑:

进行上述更改后,我通过运行您的代码得到以下输出

Heyyyy
Hi! Welcome to Notes Counter!
By Marto ©2013

        Main Menu (hint: just type the number!)

1 - Start counting
2 - View/add notes
3 - Help
4 - About
2  tttttt

You have successfully added!2       notes!

    Your notes:

1. test
2. test1
于 2013-10-08T07:17:36.617 回答