好的,所以我有一个问题,要求在main方法的1行代码中重写这段代码:
public static void main(String[] args)
String s;
boolean b;
JOptionPane jop;
jop = new JOptionPane();
s = jop.showInputDialog("Enter your email address");
b = s.matches(".*@.*\\..*");
if (b)
{
System.out.println("Address Appears Valid");
}
else
{
System.out.println("Address is Invalid");
}
}
这是我到目前为止所做的事情
public static void main( String[] args )
{
String s = JOptionPane.showInputDialog("Enter your email address");
System.out.println(s.matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );
}
如何进一步缩短此代码?感谢
答案 0 :(得分:3)
制作本:
String s = JOptionPane.showInputDialog("Enter your email address");
System.out.println(s.matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );
进入这个:
System.out.println(JOptionPane.showInputDialog("Enter your email address").matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" );
毕竟,如果您打算在下一行使用它,为什么要存储该值?
答案 1 :(得分:0)
不要在println中创建s,pur joption
答案 2 :(得分:0)
public static void main( String[] args )
{
System.out.println(JOptionPane.showInputDialog("Enter your email address").matches(".*@.*\\..*") ? "Address Appears Valid" : "Address is Invalid" ));
}
需要注意的重要一点是输入对话框返回一次只使用一次的字符串。所以不需要存储它而是在返回时使用它。
答案 3 :(得分:0)
只需从源中删除所有CR / LF字符:)