我是 Java 新手,我的班级需要编写一个程序,用户在其中输入 XXXXXXXXX 形式的社会保障代码,并以 XXXX-XX-XXX 的形式返回它。例如,如果代码是 123456789,它将作为 123-45-6789 返回给他们。
这就是我所拥有的,我想使用子字符串来完成它。
导入 java.util.Scanner;
public class HWSocialSecuritySh {
public static void main (String [] args) {
Scanner reader = new Scanner (System.in);
String name;
int ssn;
System.out.print ("Enter nine digit social security number without dashes: ");
ssn= reader.nextInt();
System.out.print("Your formatted social security number is: ");
System.out.println(snn.substring(,) );
}
}
这是新代码
导入 java.util.Scanner;
public class HWSocialSecuritySH {
public static void main (String [] args) {
Scanner reader = new Scanner (System.in);
String name;
String Ssn="ssn";
System.out.print ("Enter nine digit social security number without dashes: ");
Ssn= reader.nextLine();
System.out.print("Your formatted social security number is: ");
System.out.println (Snn.substring(0,3) +"-"snn.substring(4,6) + "-"snn.substring(7,9);
}
}