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

Game-Over 2014-04-07 03:16:14
求各位大神指导一下怎么错了,然后如何去改? 谢谢了
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);

}
}
...全文
382 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuhouqingchen_2648 2014-06-05
  • 打赏
  • 举报
回复
我不晓得
引用 5 楼 u012945394 的回复:
[quote=引用 4 楼 Xuan6251237011 的回复:] 你这赋值的风格我就看不惯了 newareacode=areacode; newnumber=number; newext=ext; ,这里应该弄反了吧
没有,他说的问题是这个 phonenumber.java:30: error: cannot find symbol invalid=true; ^ symbol: variable invalid location: class phonenumber 1 error 但是我记得这个可以的啊。然后我们运行完的结果应该是 the number is: 800-4633339 the number is: 800-4633339 invalid phone number! the number is: 0-0 the number is: 800-1234567 ext 123 the number is: 216-7654321 mynumber and mynumber2 are different the number is: 0-0 但是我做不出来invalid phone number!和 the number is: 800-1234567 ext 123 不知道怎么搞了。求大神指导并告诉我如何改,如果有代码就更好了。[/quote] 这个编译都不过啊,很明显撒没有定义,建议遇到这种问题自己找找,不要上来问,谢谢!!!
  • 打赏
  • 举报
回复
IDE的问题?如果是重启一下就搞定了
  • 打赏
  • 举报
回复
为什么不能加?
Game-Over 2014-04-07
  • 打赏
  • 举报
回复
引用 10 楼 Xuan6251237011 的回复:
你的环境确定没问题?
确定没有问题。。不能加变量在上面。所以我就再纠结。。。我把那个invalid去掉后也是运行出和你一样的。。我觉得我已经进入牛角尖里出不来了。
  • 打赏
  • 举报
回复
你的环境确定没问题?
  • 打赏
  • 举报
回复
我加多一个变量之后 private int areacode; private int number; private int ext; private boolean invalid; 能够运行输出: the number is: 800-4633339 the number is: 800-4633339 the number is: 0-0 the number is: 0-0 the number is: 216-0 mynumber and mynumber2 are different the number is: 0-0
Game-Over 2014-04-07
  • 打赏
  • 举报
回复
引用 7 楼 huadis 的回复:
你这invalid变量也没定义呀,整个程序中它在哪里再次被使用
就是invalid那个不知道怎么搞。。所以在纠结中。。
huadis 2014-04-07
  • 打赏
  • 举报
回复
你这invalid变量也没定义呀,整个程序中它在哪里再次被使用
huadis 2014-04-07
  • 打赏
  • 举报
回复
大眼看明显赋值有问题,手误?
Game-Over 2014-04-07
  • 打赏
  • 举报
回复
引用 4 楼 Xuan6251237011 的回复:
你这赋值的风格我就看不惯了 newareacode=areacode; newnumber=number; newext=ext; ,这里应该弄反了吧
没有,他说的问题是这个 phonenumber.java:30: error: cannot find symbol invalid=true; ^ symbol: variable invalid location: class phonenumber 1 error 但是我记得这个可以的啊。然后我们运行完的结果应该是 the number is: 800-4633339 the number is: 800-4633339 invalid phone number! the number is: 0-0 the number is: 800-1234567 ext 123 the number is: 216-7654321 mynumber and mynumber2 are different the number is: 0-0 但是我做不出来invalid phone number!和 the number is: 800-1234567 ext 123 不知道怎么搞了。求大神指导并告诉我如何改,如果有代码就更好了。
  • 打赏
  • 举报
回复
你这赋值的风格我就看不惯了 newareacode=areacode; newnumber=number; newext=ext; ,这里应该弄反了吧
Game-Over 2014-04-07
  • 打赏
  • 举报
回复
求各位大神来帮助我这个新手啊。。。。。。
coolbamboo2008 2014-04-07
  • 打赏
  • 举报
回复
这是外教在上课吗?哪个学校这么先进?
静山晚风 2014-04-07
  • 打赏
  • 举报
回复
这英语看的有点费劲
zpwd63 2014-04-07
  • 打赏
  • 举报
回复
引用 4 楼 Xuan6251237011 的回复:
你这赋值的风格我就看不惯了 newareacode=areacode; newnumber=number; newext=ext; ,这里应该弄反了吧
建议先把构造函数里面的赋值改了吧,确实反了!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧