我必须将其保留在案例中' A'格式我只是不明白我应该在(char c)中有什么,因为这似乎是错误的来源。如果有人有任何建议,将不胜感激。
public class PhoneNumber {
private int areacode;
private int number;
private int ext;
PhoneNumber() {
areacode = 0;
number = 0;
ext = 0;
}
PhoneNumber(int newnumber) {
areacode = 216;
number = newnumber;
ext = 0;
}
PhoneNumber(int newarea, int newnumber, int newext) {
areacode = newarea;
number = newnumber;
ext = newext;
}
PhoneNumber(String newnumber) {
String areacode = str[0];
String number = str[1];
String[] str = newnumber.split("-");
String[] number = newnumber;
boolean b1, b2;
int i = 0;
int place = 0;
for (int x: newnumber){
newnumber.charAt[i] = place;
b1 = Character.isDigit(place);
if (b1 == true){
number = place;
i++;
} else {
b2 = Character.isLetter(place);
} if (b2 == true) {
number = decode(place);
i++;
} else {
System.out.print("invalid phone number!");
}
}
System.out.print(areacode.concat(number));
return newnumber;
}
private String decode(place) {
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) {
//not complete
}
public String toString() {
//not complete
}
}
这是错误:
G:\CIS260\Assignments>javac PhoneNumber.java
PhoneNumber.java:53: error: <identifier> expected
private String decode(place) {
^
1 error
答案 0 :(得分:1)
你有一个错误突然出现在我面前。请注意,如果您遇到语法错误,大多数编译器都会惊慌失措并将所有错误标记为实际错误。调试意外语法错误的最佳方法是查看第一个错误:)
boolean = b1, b2;
没有任何意义。变量声明如下
[Type] [Variable Name] | ,[Additional Vars];
所以它应该是boolean b1, b2;
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 "";
}
}
我建议您使用查找表(hashmap)来完成这组代码,它看起来不那么难看,而且眼睛疼得容易。
一个例子是
private static final Map<Char, String> myDecodeLookup = new Hashmap<Char,String>();
static{ //initializer block
myDecodeLookup.put('A', "2");
myDecodeLookup.put('B', "2");
//and so and and so forth
}
private String decode(char c) throws KeyNotFoundException{
return MyClassName.myDecodeLookup.get(c); //you should code this to support the keynotfoundexception that this might throw, or not.
}
如果你发现自己调试语法错误 5个小时,你应该退后一步,查找语言结构本身的语法......这是一个很好的建议继续前进。