<p><font face="宋体" size="3">
<br></font></p><p><span>提示用户输入一个社保号码,它的格式是</span><span lang="EN-US"><font face="Calibri">DDD-DD-DDDD</font></span><span>,其中</span><span lang="EN-US"><font face="Calibri">D</font></span><span>是一个数字。编程判断输入是否合法。</span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri">import java.util.Scanner;</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri">public class ExamDemo {</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>public
static void main(String[] args) {</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>Scanner
input = new Scanner(System.in);</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>System.out.print("Enter
a SSN: ");</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>String
ssn = input.nextLine();</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>boolean
isValid = ssn.length() == 11 && Character.isDigit(ssn.charAt(0))
&&</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>Character.isDigit(ssn.charAt(1))
&& Character.isDigit(ssn.charAt(2)) && </font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>ssn.charAt(3)
== '-' && Character.isDigit(ssn.charAt(4)) && </font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>Character.isDigit(ssn.charAt(5))
&& ssn.charAt(6) == '-' && </font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>Character.isDigit(ssn.charAt(7))
&& Character.isDigit(ssn.charAt(8)) && </font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>Character.isDigit(ssn.charAt(9))
&& Character.isDigit(ssn.charAt(10));</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>if
(isValid)</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>System.out.println(ssn
+ " is a valid social security number");</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>else</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span><span> </span>System.out.println(ssn + " is
an invalid social security number");</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri"><span> </span>}</font></span></p><p><font face="宋体" size="3">
<br></font></p><p><span lang="EN-US"><font face="Calibri">}</font></span></p><p><font face="宋体" size="3">
<br></font></p>