双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

求大神帮忙。一直没搞明白怎么错了

Game-Over Game-Over 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
求各位大神指导一下怎么错了,然后如何去改? 谢谢了
1. Your PhoneNumber class has and only has 3 int variables in its data field (please do not add any other data variables):

int areacode;
int number;
int ext;

2. Your PhoneNumber class has 4 constructors with the specifications as below:

if no argument is given, all data fieds (areacode, number, ext) are initialized to zero;
if one int argument is given, the constructor initializes number by using this argument, and set default areacode to 216, and default ext to 0.
if three int arguments are given, initialize areacode, number, ext accordingly.
if one String argument "xxx-xxxxxxx" is given, where the first three characters are numbers and the last 7 characters are either numbers or letters, e.g., "800-GOFEDEX", convert the String to numbers and initialize the data field accordingly. Note the ext should be initialized to 0.

Your program can assume the String is delimited by "-". If the character is a letter, your program should convernt it to a number according to the key pad standard. If the character is neither a number nor a letter, your contrustor will output an error message and initialize the data fileds to zeros.
Hint: given a String, you may use charAt(index) method to extract the character one by one. The decoding method that converts a letter to the corresponding number is provided. In addition, you may find the following methods useful:

Character.isLetterOrDigit(char c): returns either true or false.

Character.isDigit(char c): returns either true or false.

String.valueOf(char digit): convert a character to a String.

String.valueOf(int number): convert an int to a String.

Integer.parseInt(String str): convert a String to an int.

3. Your PhoneNumber provides a method equals() which compares the current object to another PhoneNumber object.
4. Your PhoneNumber provides a method toString() which outputs a String format of the object.



public class phonenumber{
private int areacode;
private int number;
private int ext;

phonenumber(){
areacode=0;
number=0;
ext=0;
}

phonenumber(int newnumber){
areacode=216;
newnumber=number;
ext=0;
}
phonenumber(int newareacode, int newnumber, int newext){
newareacode=areacode;
newnumber=number;
newext=ext;
}

phonenumber(String newnumber){
ext=0;
String[] str=newnumber.split("-");
StringBuilder nun=new StringBuilder();
for(int i=0; i<7; i++){
char c=str[1].charAt(i);
if (!((c <=90 && c>=65)||(c>=48 && c<=57))){
invalid=true;
return;
}
if(c<=90 && c>=65){
nun.append(decode(c));
}else
{nun.append(c);}
}
number=Integer.parseInt(nun.toString());
areacode=Integer.parseInt(str[0]);



}
private String decode(char c){
switch (c){
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'Z': return "9";
default:return "";
}}
public boolean equals(phonenumber pn){
return (areacode==pn.areacode && number==pn.number && ext==pn.ext);
}
public String toString(){
if(ext==0){
return areacode+"-"+number;
}else{
return areacode+"-"+number+" ext "+ext;
}

}}



public class phone {
public static void main (String[] args) {

phonenumber mynumber = new phonenumber("800-GOFEDEX");
System.out.println("the number is: " + mynumber);

phonenumber mynumber2 = new phonenumber("800-46FEDEX");
System.out.println("the number is: " + mynumber2);

phonenumber mynumber3 = new phonenumber("800-&*(^&%@");
System.out.println("the number is: " + mynumber3);

mynumber = new phonenumber(800, 1234567, 123);
System.out.println("the number is: " + mynumber);

mynumber2 = new phonenumber(7654321);
System.out.println("the number is: " + mynumber2);

if (mynumber.equals(mynumber2))
System.out.println("mynumber and mynumber2 are the same");
else
System.out.println("mynumber and mynumber2 are different");

mynumber = new phonenumber();
System.out.println("the number is: " + mynumber);

}
}
...全文
评论 16
收藏 已收藏
分享
返回顶部
Game-Over
Java SE 62,621 成员 307,251 内容