Java 使用 JOptionPane 显示数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19917221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying Array with JOptionPane
提问by jarnthrax
First, this is a school activity, an activity that i cant solve by my own.
首先,这是一个学校活动,一个我自己解决不了的活动。
i'm a first year college BSIT student and this my first post.
我是大学 BSIT 的一年级学生,这是我的第一篇文章。
i'm having trouble in displaying my "initialized array" in JOptionPane, i cant find the right code to display them. hope you get my question.
我在 JOptionPane 中显示我的“初始化数组”时遇到问题,我找不到正确的代码来显示它们。希望你能得到我的问题。
heres my code..
继承人我的代码..
public static void main(String[] args) {
String display="";
String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
int newsize = Integer.parseInt(size);
JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");
String array[] = new String[newsize];
for (int a=0; a<array.length;a++)
{
JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
}
for (int a=0;a<array.length;a++)
{
display = display +array[a];
}
JOptionPane.showMessageDialog(null,"\n"+array[display]);
}
}
采纳答案by Tomas Bisciak
JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
shoud be
应该是
array[a]=JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
and then make string
然后制作字符串
for (int a=0;a<array.length;a++)
{
display+=array[a]+","
}
after that just print it out :)
之后就打印出来:)
for new line display+=array[a]+"\n"
对于新线 display+=array[a]+"\n"
回答by Daniel Hernández
you could do that in 1 loop.
你可以在 1 个循环中做到这一点。
For example:
例如:
public static void main(String[] args) {
公共静态无效主(字符串 [] args){
String display="";
String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
int newsize = Integer.parseInt(size);
JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");
String array[] = new String[newsize];
for (int a=0; a<array.length;a++)
{
array[a] = JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
display = display +array[a] + "\n";
}
JOptionPane.showMessageDialog(null,display);
}
}
}