过去几个月你们都得到了很多帮助!
我有一个关于JOptionPane和写入控制台的快速问题。有没有办法运行程序,每次运行时,它都会添加到控制台窗口?在它的位置,我的代码将删除控制台窗口中的任何内容,并在我编写时添加新信息。
我的代码如下:
import javax.swing.JOptionPane;
public class Contacts {
private static java.lang.String String;
public static <NO_OPTION> void main(String[] args) {
// contact name
String name;
// contact number
String number;
{
String = JOptionPane
.showInputDialog("What is the Contact's Full Name?");
name = String;
String = JOptionPane
.showInputDialog("What is the Contact's Phone Number?");
number = String;
JOptionPane.showMessageDialog(null, "Contact Added!");
System.out.append("Contact List");
System.out.println();
System.out.println("--------------------------------");
System.out.append(name + " " + " " + number);
}
}
}
答案 0 :(得分:0)
希望这会对你有所帮助
如果要保存数据,则必须使用数据库。或者,如果您想在程序存活期间保存数据,则可以使用
ArrayList
。
<强> 编辑: 强>
import java.util.ArrayList;
import javax.swing.JOptionPane;
class Contacts {
public static void main(String[] args) {
String name;
// contact number
String number;
ArrayList<String> nameList = new ArrayList<String>();
ArrayList<String> numberList = new ArrayList<String>();
while (true) {
name = JOptionPane
.showInputDialog("What is the Contact's Full Name? Press cancle to exit");
if (name == null) {// terminate loop if name is not provided
break;
}
number = JOptionPane
.showInputDialog("What is the Contact's Phone Number? Press cancle to exit");
if (number == null) {// terminate loop if number is not provided
break;
}
nameList.add(name);
numberList.add(number);
JOptionPane.showMessageDialog(null, "Contact Added!");
System.out.print("\nContact List");
System.out.println();
for (int i = 0; i < nameList.size(); i++) {
System.out.println("--------------------------------");
System.out.println(nameList.get(i) + " " + " "
+ numberList.get(i));
}
}
}
}