Java 您可以在一个作业窗格输入中向数组中输入多个字段吗?
我正在尝试创建一个多数组设置,其中我有来自一个JOption面板的书籍作者、标题和页面计数所有输入,新填充的数组将用作单个书籍数组设置的源。这是我在atm机上的密码,这可能吗?我可以进入窗格进行输入,但输入数据后立即出现错误(线程“main”java.lang.RuntimeException中的异常:不可编译的源代码-错误的ctor sym类型: 在librarybook.LibraryBookSorting.main(LibraryBookSorting.java:43) ~\8.2\executor snippets\run.xml:53:Java返回:1) 我当前启动的代码如下:Java 您可以在一个作业窗格输入中向数组中输入多个字段吗?,java,arrays,joptionpane,Java,Arrays,Joptionpane,我正在尝试创建一个多数组设置,其中我有来自一个JOption面板的书籍作者、标题和页面计数所有输入,新填充的数组将用作单个书籍数组设置的源。这是我在atm机上的密码,这可能吗?我可以进入窗格进行输入,但输入数据后立即出现错误(线程“main”java.lang.RuntimeException中的异常:不可编译的源代码-错误的ctor sym类型: 在librarybook.LibraryBookSorting.main(LibraryBookSorting.java:43) ~\8.2\exe
public class LibraryBookSorting {
static LibraryBook[] myBook = new LibraryBook[5];
static String title[5];
static String author[5];
static String count[5];
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
JTextField Title = new JTextField(5);
JTextField Author = new JTextField(5);
JTextField pgCount = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Title:"));
myPanel.add(Title);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("Author:"));
myPanel.add(Author);
myPanel.add(new JLabel("PageCount: "));
myPanel.add(pgCount);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter Title, Author and Page Count.",
JOptionPane.OK_CANCEL_OPTION);
title[i] = Title.getText();
author[i] = Author.getText();
count[i] = pgCount.getText();
myBook[0] = new LibraryBook(title[0], author[0], count[0]);
myBook[1] = new LibraryBook(title[1], author[1], count[1]);
myBook[2] = new LibraryBook(title[2], author[2], count[2]);
myBook[3] = new LibraryBook(title[3], author[3], count[3]);
myBook[4] = new LibraryBook(title[4], author[4], count[4]);
for (int i = 0; i < myBook.length; i++) {
myBook.toString();
}
}
}
您的LibraryBookSorting类有许多错误,例如数组声明错误,并且i被用于两个不同的循环,一旦这些错误得到修复,您的代码就可以正常工作。请查看以下代码:
public class LibraryBookSorting {
static LibraryBook[] myBook = new LibraryBook[5];
static String title[] = new String[5];
static String author[] = new String[5];
static String count[] = new String[5];
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
JTextField Title = new JTextField(5);
JTextField Author = new JTextField(5);
JTextField pgCount = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Title:"));
myPanel.add(Title);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("Author:"));
myPanel.add(Author);
myPanel.add(new JLabel("PageCount: "));
myPanel.add(pgCount);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter Title, Author and Page Count.",
JOptionPane.OK_CANCEL_OPTION);
title[i] = Title.getText();
author[i] = Author.getText();
count[i] = pgCount.getText();
myBook[0] = new LibraryBook(title[0], author[0], count[0]);
myBook[1] = new LibraryBook(title[1], author[1], count[1]);
myBook[2] = new LibraryBook(title[2], author[2], count[2]);
myBook[3] = new LibraryBook(title[3], author[3], count[3]);
myBook[4] = new LibraryBook(title[4], author[4], count[4]);
}
for (int j = 0; j < myBook.length; j++) {
System.out.println(myBook[j].toString());
}
}
}
公共类图书馆图书分类{
静态LibraryBook[]myBook=新LibraryBook[5];
静态字符串标题[]=新字符串[5];
静态字符串作者[]=新字符串[5];
静态字符串计数[]=新字符串[5];
公共静态void main(字符串[]args){
对于(int i=0;i<5;i++){
JTextField Title=新的JTextField(5);
JTextField Author=新的JTextField(5);
JTextField pgCount=新的JTextField(5);
JPanel myPanel=新的JPanel();
添加(新JLabel(“标题:”);
myPanel.add(标题);
myPanel.add(Box.createHorizontalStruct(15));//一个垫片
添加(新JLabel(“作者:”);
添加(作者);
添加(新的JLabel(“页面计数:”);
myPanel.add(pgCount);
int result=JOptionPane.showConfirmDialog(null,myPanel,
“请输入标题、作者和页数。”,
JOptionPane.OK\u CANCEL\u选项);
title[i]=title.getText();
author[i]=author.getText();
count[i]=pgCount.getText();
myBook[0]=新图书馆图书(标题[0],作者[0],计数[0]);
myBook[1]=新图书馆图书(标题[1],作者[1],计数[1]);
myBook[2]=新图书馆图书(标题[2],作者[2],计数[2]);
myBook[3]=新图书馆图书(标题[3],作者[3],计数[3]);
myBook[4]=新图书馆图书(标题[4],作者[4],计数[4]);
}
for(int j=0;j
请修复缩进,以便显示的代码可读。另外,显示实际代码。显示的代码不可编译(例如,main()方法引用了一个名为“count”的变量,但代码中没有声明这样的变量)。
public class LibraryBookSorting {
static LibraryBook[] myBook = new LibraryBook[5];
static String title[] = new String[5];
static String author[] = new String[5];
static String count[] = new String[5];
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
JTextField Title = new JTextField(5);
JTextField Author = new JTextField(5);
JTextField pgCount = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("Title:"));
myPanel.add(Title);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("Author:"));
myPanel.add(Author);
myPanel.add(new JLabel("PageCount: "));
myPanel.add(pgCount);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter Title, Author and Page Count.",
JOptionPane.OK_CANCEL_OPTION);
title[i] = Title.getText();
author[i] = Author.getText();
count[i] = pgCount.getText();
myBook[0] = new LibraryBook(title[0], author[0], count[0]);
myBook[1] = new LibraryBook(title[1], author[1], count[1]);
myBook[2] = new LibraryBook(title[2], author[2], count[2]);
myBook[3] = new LibraryBook(title[3], author[3], count[3]);
myBook[4] = new LibraryBook(title[4], author[4], count[4]);
}
for (int j = 0; j < myBook.length; j++) {
System.out.println(myBook[j].toString());
}
}
}