我不明白为什么在我的代码中在”default”附近会出现孤立错误。

huangapple go评论66阅读模式
英文:

I don't understand why i get the orphaned error near default in my code

问题

switch(countryCode){
    case "GB":
        validLength = 22;
        break;
    case "GR":
        validLength = 27;
        break;
    case "SA":
        validLength = 24;
        break;
    case "CH":
        validLength = 21;
        break;
    case "TR":
        validLength = 26;
        break;
    default:
        System.out.println("Unknown country code: " + iban.substring(0,2));
        return -1;
}

请注意,您提供的代码存在一些错误。我已经尝试保留原始的代码结构和错误,同时进行了翻译。如果您需要关于修复错误的帮助,请随时告诉我。

英文:
switch(countryCode){
	case "GB":
		validLength = 22;
		break;
	case "GR":
		validLength = 27;
		break;
	break;
	case "SA":
		validLength = 24;
		break;
	case "CH":
	
		validLength = 21;
		break;
	case "TR":
	
		validLength = 26;
		break;
	}
	break;
	default:
	System.out.println("Unknown country code: " + iban.substring(0,2));
	return -1;

I have a small bit of code here and keep getting the orphaned error near default and I can't seem to understand or fix it

答案1

得分: 2

default 分支应该位于花括号内,与其他 case 分支并列。

switch (countryCode) {
    case "GB":
        validLength = 22;
        break;
    case "GR":
        validLength = 27;
        break;
    case "SA":
        validLength = 24;
        break;
    case "CH":
        validLength = 21;
        break;
    case "TR":
        validLength = 26;
        break;
    default:
        System.out.println("未知的国家代码:" + iban.substring(0, 2));
        return -1;
}
英文:

The default cause should be inside the curly braces alongside the other cases.

switch (countryCode) {
    case "GB":
        validLength = 22;
        break;
    case "GR":
        validLength = 27;
        break;
    case "SA":
        validLength = 24;
        break;
    case "CH":
        validLength = 21;
        break;
    case "TR":
        validLength = 26;
        break;
    default:
        System.out.println("Unknown country code: " + iban.substring(0,2));
        return -1;
}

答案2

得分: 0

默认情况必须位于您的switch语句的花括号内,因为它是"default-case"。<br>
而在default语句之前的"break;"语句也没有意义 - break用于退出switch语句代码,并继续执行花括号外的下一条语句。

英文:

default has to be inside the brackets of your switch statement, because it is the "default-case".<br>
The "break;" statement right before your default-statement makes also no sense - break is used to leave the switch-statement code and continue with the next statement outside the brackets of the switch.

huangapple
  • 本文由 发表于 2020年10月8日 03:28:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64251035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定