gpt4 book ai didi

java - 解决代码中的错误

转载 作者:行者123 更新时间:2023-12-01 13:52:16 25 4
gpt4 key购买 nike

我似乎无法找出这段代码中的问题。当我尝试运行时,它一直说:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
Syntax error, insert "}" to complete MethodBody
Syntax error, insert "}" to complete ClassBody

但是当我尝试将 } 添加到代码中时,它仍然无法解决错误。

这是代码:

public String checkPostalCode (String state, int postalno) {

String postalno1 = Integer.toString(postalno);
String result = "";

String BELAIT = "^[3000-3999](1,4}$";
String TUTONG = "^[2600-2618][2900-2920]{1,4}$";
String BANDAR = "^[2000-2599][2619-2898][2921-2999]{1,4}$";
String TEMBURONG = "^[4000-4999]{1,4}$";


if(state == "Belait") {
Pattern chckPostalCode = Pattern.compile(BELAIT);
Matcher chckPostalCodematcher=chckPostalCode.matcher(postalno1);

boolean Belait = chckPostalCodematcher.matches();
if(Belait==true){
result = "pass";
}
else{
result = "fail";
}
if(state == "Tutong") {
Pattern chckPostalCode1 = Pattern.compile(TUTONG);
Matcher chckPostalCodematcher1=chckPostalCode.matcher(postalno1);

boolean Tutong = chckPostalCodematcher.matches();
if(Tutong==true){
result = "pass";
}
else{
result = "fail";
}

if(state == "Bandar Seri Begawan") {
Pattern chckPostalCode2 = Pattern.compile(BANDAR);
Matcher chckPostalCodematcher2=chckPostalCode.matcher(postalno1);

boolean Bandar = chckPostalCodematcher.matches();
if(Bandar==true){
result="pass";
}
else{
result = "fail";
}

if(state == "Temburong") {
Pattern chckPostalCode3 = Pattern.compile(TEMBURONG);
Matcher chckPostalCodematcher3=chckPostalCode.matcher(postalno1);

boolean Temburong = chckPostalCodematcher.matches();
if(Temburong==true){
result="pass";
}
else{
result = "fail";
}


return result;

}

}
return result;
}
}
}))

最佳答案

您忘记在这里关闭大括号:

if(Belait==true){
result = "pass";
} // you forgot this

同样适用于 if(Tutong==true)if(Bandar==true)

始终使用 IDE 来避免此类拼写错误。

关于java - 解决代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com