本文发表在 rolia.net 枫下论坛A.
public class TelephoneFormat
{
private String telephone;
public TelephoneFormat(String t)
{
telephone = t;
}
public String getAreaCode()
{
return telephone.substring(0,3);
}
public String getExchangeNumber()
{
return telephone.substring(3,6);
}
public String getNumber()
{
return telephone.substring(6,10);
}
public void setTelephoneNumber(String t)
{
this.telephone = t;
}
}
B.
import java.io.*;
public class TestTelephoneFormat
{
public static void main (String[] args) throws IOException
{
TelephoneFormat myTelephone;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter 10-digit telephone number:");
String input = console.readLine();
myTelephone = new TelephoneFormat(input);
myTelephone.setTelephoneNumber(input);
System.out.print("The area code is: = " + myTelephone.getAreaCode()+"\n");
System.out.print("The exchange is: = " + myTelephone.getExchangeNumber()+"\n");
System.out.print("The number is: = " + myTelephone.getNumber()+"\n");
System.out.print("The complete telephone number is: =" + "(" + myTelephone.getAreaCode() + ")"
+ myTelephone.getExchangeNumber() + "-" + myTelephone.getNumber()+"\n");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
public class TelephoneFormat
{
private String telephone;
public TelephoneFormat(String t)
{
telephone = t;
}
public String getAreaCode()
{
return telephone.substring(0,3);
}
public String getExchangeNumber()
{
return telephone.substring(3,6);
}
public String getNumber()
{
return telephone.substring(6,10);
}
public void setTelephoneNumber(String t)
{
this.telephone = t;
}
}
B.
import java.io.*;
public class TestTelephoneFormat
{
public static void main (String[] args) throws IOException
{
TelephoneFormat myTelephone;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter 10-digit telephone number:");
String input = console.readLine();
myTelephone = new TelephoneFormat(input);
myTelephone.setTelephoneNumber(input);
System.out.print("The area code is: = " + myTelephone.getAreaCode()+"\n");
System.out.print("The exchange is: = " + myTelephone.getExchangeNumber()+"\n");
System.out.print("The number is: = " + myTelephone.getNumber()+"\n");
System.out.print("The complete telephone number is: =" + "(" + myTelephone.getAreaCode() + ")"
+ myTelephone.getExchangeNumber() + "-" + myTelephone.getNumber()+"\n");
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net