问题详情

提示用户输入一个社保号码,它的格式是DDD-DD-DDDD,其中D是一个数字。编程判断输入是否合法。


时间:2021-07-17 17:55 关键词:

答案解析

<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>boolean isValid = ssn.length() == 11 &amp;&amp; Character.isDigit(ssn.charAt(0)) &amp;&amp;</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Character.isDigit(ssn.charAt(1)) &amp;&amp; Character.isDigit(ssn.charAt(2)) &amp;&amp; </font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>ssn.charAt(3) == '-' &amp;&amp; Character.isDigit(ssn.charAt(4)) &amp;&amp; </font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Character.isDigit(ssn.charAt(5)) &amp;&amp; ssn.charAt(6) == '-' &amp;&amp; </font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Character.isDigit(ssn.charAt(7)) &amp;&amp; Character.isDigit(ssn.charAt(8)) &amp;&amp; </font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Character.isDigit(ssn.charAt(9)) &amp;&amp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if (isValid)</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>else</font></span></p><p><font face="宋体" size="3"> <br></font></p><p><span lang="EN-US"><font face="Calibri"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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>