这是在 Java 中使用 'goto' 的有效案例吗
Is this a valid case for use of a 'goto' in Java
我非常清楚 'goto' 在结构化编程的大多数专业追随者的词典中是一个肮脏的词。所以我想问一下如何替换 'goto' 的以下用法。
我正在编写桥牌竞价应用程序。我问用户"Who dealt"。如果他这样做了,没问题。如果他右边的玩家发牌(东),则必须询问用户该玩家的出价。如果对面的玩家发牌(伙伴),则必须询问用户对面的玩家叫什么,然后是东。如果用户左边的玩家发牌(西),则必须依次询问用户所有这三个玩家的出价。
我有一个显示屏幕的 activity "What bid",并通过 "West"、"your partner" 或 "East" 取决于谁出价。 activity 是使用 startActivityForResult 和 returns 进行的出价调用的。事情是这样的。我认为这是最有效的方法:
if (whodealt == "West") {continue}
else if (whodealt == "North") {goto northbid;}
else if (whodealt == "East") {goto eastbid;}
westbid:
startActivityForResult() etc. passing "West";
process result (e.g. store bid)
northbid:
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
eastbid:
startActivityForResult() etc. passing "East";
process result (e.g. store bid)
显然,我已经简化了一切。但是下面的替代非goto结构是不是有点麻烦,比如
if (whodealt == "West") {
startActivityForResult() etc. passing "West";
process result (e.g. store bid)
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
else if (whodealt == "North") {
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
else if (whodealt == "East") {
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
人们对此有何看法?
您所描述的本质上是一个带有 fallthroughs 的 switch 语句:
switch(whodealt) {
case WEST:
// startActivityForResult() etc. passing WEST;
// process result
// fall through
case NORTH:
// startActivityForResult() etc. passing NORTH;
// process result
// fall through
case EAST:
// startActivityForResult() etc. passing EAST;
// process result
// fall through
case SOUTH: // (the player dealt themselves)
break;
default:
throw new IllegalArgumentException("Unrecognised Dealer");
}
Java 从 Java 7 开始支持 switch 语句中的字符串,但我建议不要为此使用字符串,并为经销商定义自己的枚举:在这种情况下,只有一个whodealt
可以容纳的值的有限域,它比所有字符串的域小得多。
Java 没有 goto 语句。 goto 是 Java 中的保留关键字,但该语言未使用它。请参阅此问题以获得更完整的答案。
Is there a goto statement in Java?
要回答您的问题 "Is this a valid case for use of a 'goto' in Java",答案必须是否定的,因为在 Java 中是不可能的。
If the player on his right dealt (East), then the user must be asked what that >player bid. If the player opposite dealt (partner), the user must be asked what >the player opposite bid and then what East. If the player on the user's left >dealt (West), the user must be asked, in turn, what all those three players bid.
对于责任链来说,这似乎是一个很好的情况,尤其是当您想实现更多逻辑代码时。否则我会建议像 @amnn.
这样的 switch case 语句
我非常清楚 'goto' 在结构化编程的大多数专业追随者的词典中是一个肮脏的词。所以我想问一下如何替换 'goto' 的以下用法。
我正在编写桥牌竞价应用程序。我问用户"Who dealt"。如果他这样做了,没问题。如果他右边的玩家发牌(东),则必须询问用户该玩家的出价。如果对面的玩家发牌(伙伴),则必须询问用户对面的玩家叫什么,然后是东。如果用户左边的玩家发牌(西),则必须依次询问用户所有这三个玩家的出价。
我有一个显示屏幕的 activity "What bid",并通过 "West"、"your partner" 或 "East" 取决于谁出价。 activity 是使用 startActivityForResult 和 returns 进行的出价调用的。事情是这样的。我认为这是最有效的方法:
if (whodealt == "West") {continue}
else if (whodealt == "North") {goto northbid;}
else if (whodealt == "East") {goto eastbid;}
westbid:
startActivityForResult() etc. passing "West";
process result (e.g. store bid)
northbid:
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
eastbid:
startActivityForResult() etc. passing "East";
process result (e.g. store bid)
显然,我已经简化了一切。但是下面的替代非goto结构是不是有点麻烦,比如
if (whodealt == "West") {
startActivityForResult() etc. passing "West";
process result (e.g. store bid)
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
else if (whodealt == "North") {
startActivityForResult() etc. passing "your partner";
process result (e.g. store bid)
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
else if (whodealt == "East") {
startActivityForResult() etc. passing "East";
process result (e.g. store bid) }
人们对此有何看法?
您所描述的本质上是一个带有 fallthroughs 的 switch 语句:
switch(whodealt) {
case WEST:
// startActivityForResult() etc. passing WEST;
// process result
// fall through
case NORTH:
// startActivityForResult() etc. passing NORTH;
// process result
// fall through
case EAST:
// startActivityForResult() etc. passing EAST;
// process result
// fall through
case SOUTH: // (the player dealt themselves)
break;
default:
throw new IllegalArgumentException("Unrecognised Dealer");
}
Java 从 Java 7 开始支持 switch 语句中的字符串,但我建议不要为此使用字符串,并为经销商定义自己的枚举:在这种情况下,只有一个whodealt
可以容纳的值的有限域,它比所有字符串的域小得多。
Java 没有 goto 语句。 goto 是 Java 中的保留关键字,但该语言未使用它。请参阅此问题以获得更完整的答案。
Is there a goto statement in Java?
要回答您的问题 "Is this a valid case for use of a 'goto' in Java",答案必须是否定的,因为在 Java 中是不可能的。
If the player on his right dealt (East), then the user must be asked what that >player bid. If the player opposite dealt (partner), the user must be asked what >the player opposite bid and then what East. If the player on the user's left >dealt (West), the user must be asked, in turn, what all those three players bid.
对于责任链来说,这似乎是一个很好的情况,尤其是当您想实现更多逻辑代码时。否则我会建议像 @amnn.
这样的 switch case 语句