我在开始Java。我已经尝试了好几天,想弄清楚如何编写以下代码:
使用while循环使用单个JOptionPane请求以空格分隔的姓名、电话和电子邮件。
在循环中,检查用户是否选择OK或Cancel而不输入数据,如果是,提示用户直到输入有效数据。
将name, phone, email分隔成单独的String变量
在循环中,检查名称是否少于10个字符,如果不是,提示用户直到输入有效的数据。
如果输入了有效的数据,则使用构造函数和用户提供的姓名、电话和电子邮件创建Contact对象。使用get方法在JOptionPane中显示内容。
即使有人可以帮我用"使用while循环询问姓名,电话和电子邮件,使用单个JOptionPane以空格分隔。"
和
"如果输入了有效的数据,使用构造函数和用户提供的姓名、电话和电子邮件创建Contact对象"代码。
其余的我肯定能算出来。是的,我知道如何拼写向日葵…A是故意的。谢谢任何帮助我的人。我真的很感激!
这就是我所拥有的:(联系人类信息张贴在下面)我正在学习如何清理这些代码以提高效率。在我弄清楚如何执行while循环之后,我打算从第19行开始删除。现在,当我真正让代码工作时,我感到非常兴奋。
进口javax.swing。*;公共类TestContact{
public static void main(String[] args)
{
Contact mycontact = new Contact();
mycontact.setName("Tanya Smith");
mycontact.setPhone("440-226-2866");
mycontact.setEmail("tanya@gmail.com");
JOptionPane.showMessageDialog(null,
"The Contact's information is:n Name: " + mycontact.getName() +
"n Phone Number: " + mycontact.getPhone () +
"n Email: " + mycontact.getEmail());
JOptionPane.showInputDialog(null, "Please enter your Name: " );
while Name.equals()
String Info = JOptionPane.showInputDialog(null, "Please enter you Name, Phone Number and Email");
String[] word = Info.split(" ");
String AllInfo =
Character.toString(word[0].charAt(0)) +
Character.toString(word[1].charAt(0)) +
Character.toString(word[2].charAt(0)) +
Character.toString(word[3].charAt(0));
JOptionPane.showMessageDialog(null, "Your Name: " + word[0] + " " + word[1] +
"nYour Phone: " + word[2] +
"nYour Email: " + word[3]);
}
}
我想出了如何用一种方式来处理角色。
虽然我不赞成简单地要代码,但你确实听起来很困惑。我还在等待一个非常慢的网络连接上的4g传输,所以这里有一点可以让你开始。这应该能帮你完成大部分任务。下次试着发布你想到的任何东西,不管你认为它有多离谱。至少我们知道你不只是要代码。
public static void main(String [] args) {
promptForData();
}
public static void promptForData() {
boolean cont = true;
while (cont) {
String input = JOptionPane.showInputDialog("Enter name phone and email space delimited.");
cont = !validData(input);
}
}
public static boolean validData(String input) {
String[] parts = input.split(" ");
if (parts.length != 3) return false;
if (parts[0].length() < 11) return false;
return true;
}
伪代码"即使有人可以帮助我只是"使用一个while循环询问姓名,电话和电子邮件由空格分隔使用单个JOptionPane。"
String name;
String phone;
String email;
String input;
String[] array;
while name.length() > 10 or name is null
input = JOptionPane...
array = input.split(....)
name = first array index
// end loop
phone = second array index
email = third array index
伪代码"如果输入了有效的数据,使用构造函数和用户提供的姓名、电话和电子邮件创建Contact对象。"
class Contact
String name;
String phone;
String email
Contact (constructor taking the three field type arg)
this field = an argument
.... // two more
从第一部分循环后得到有效的输入
Contact contact = new Contact( fill in the args)