有没有办法重构这些 if 语句?
Is there a way to refactor these if statements?
只是想知道是否有重构以下代码的方法?我是 Java 的新手,正在尝试使用 DRY 代码 - 下面是我写的,但似乎有很多条件需要检查
void printDirection() {
if (yDirection > 0) {
if (xDirection < 0) {
println("Travelling South-West");
} else {
println("Travelling South-East");
}
} else if (yDirection < 0) {
if (xDirection <0) {
println("Travelling North-West");
} else {
println("Travelling North-East");
}
}
}
在此先感谢您的帮助!
您可以分别评估 north/south 和 east/west 条件,并将说明粘贴到您的消息中。
System.out.printf("Travelling %s-%s%n", (yDirection < 0 ? "North" : "South"),
(xDirection < 0 ? "West" : "East"));
根据你问题中的代码,我假设你只关心这四个互补方向(不是正北、正东、静止等)。
如果你真的想让它变干,可以使用运算符 ?但它既不容易阅读也不推荐。它用于以尽可能快的速度为目标的编程竞赛。
它遵循以下方案:
(条件?WhatHappenIfConditionIsTrue:WhatHappenIfConditionIsFalse);
您可以在作业中使用它:
int i = (a>0)?a:0;
在那种情况下,如果a>0则i=a,否则a=0
在你的情况下,我会这样做
void printDirection()
{
System.out.println("Travelling " + (yDirection > 0?"South":"North") + "-" + (xDirection>0?"East":"West"));
}
一些建议:
1. 由于x,y组合;有五个州;您可以使用枚举类型来定义这些状态;
2. 如果你想减少代码中的if...else语句,请参考Status Machine Design Pattern;不过我觉得,在你的情况下,状态就这么简单,没必要搞得太复杂
public class Status {
public enum Direction {
SOUTH_WEST((x, y) -> y > 0 && x < 0, "Travelling South-West")
, SOUTH_EAST((x, y) -> y >0 && x > 0, "Travelling South-East")
, NORTH_EAST((x, y) -> x > 0 && y < 0, "Travelling North-East")
, NORTH_WEST((x,y) -> x < 0 && y < 0, "Travelling North-West"), CENTER((x,y) -> x == 0 && y == 0, "");
BiPredicate<Integer, Integer> bp;
String desc;
public BiPredicate<Integer, Integer> getBp() {
return bp;
}
public void setBp(BiPredicate<Integer, Integer> bp) {
this.bp = bp;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private Direction(BiPredicate<Integer, Integer> bp, String desc) {
this.bp = bp;
this.desc = desc;
}
public static Direction getDirection(int x, int y) {
for (Direction direction : Direction.values()) {
if(direction.getBp().test(x, y)) {
return direction;
}
}
return null;
}
}
public static void main(String[] args) {
Direction d = Direction.getDirection(3, 4);
System.out.println(d.getDesc());
/* if(d == Direction.SOUTH_WEST){
System.out.println("do some thing");
} else if(d == Direction.SOUTH_EAST){
System.out.println("do some thing");
} else if(d == Direction.NORTH_EAST){
System.out.println("do some thing");
} else if(d == Direction.NORTH_WEST){
System.out.println("do some thing");
}*/
}
}
只是想知道是否有重构以下代码的方法?我是 Java 的新手,正在尝试使用 DRY 代码 - 下面是我写的,但似乎有很多条件需要检查
void printDirection() {
if (yDirection > 0) {
if (xDirection < 0) {
println("Travelling South-West");
} else {
println("Travelling South-East");
}
} else if (yDirection < 0) {
if (xDirection <0) {
println("Travelling North-West");
} else {
println("Travelling North-East");
}
}
}
在此先感谢您的帮助!
您可以分别评估 north/south 和 east/west 条件,并将说明粘贴到您的消息中。
System.out.printf("Travelling %s-%s%n", (yDirection < 0 ? "North" : "South"),
(xDirection < 0 ? "West" : "East"));
根据你问题中的代码,我假设你只关心这四个互补方向(不是正北、正东、静止等)。
如果你真的想让它变干,可以使用运算符 ?但它既不容易阅读也不推荐。它用于以尽可能快的速度为目标的编程竞赛。
它遵循以下方案: (条件?WhatHappenIfConditionIsTrue:WhatHappenIfConditionIsFalse); 您可以在作业中使用它:
int i = (a>0)?a:0;
在那种情况下,如果a>0则i=a,否则a=0
在你的情况下,我会这样做
void printDirection()
{
System.out.println("Travelling " + (yDirection > 0?"South":"North") + "-" + (xDirection>0?"East":"West"));
}
一些建议: 1. 由于x,y组合;有五个州;您可以使用枚举类型来定义这些状态; 2. 如果你想减少代码中的if...else语句,请参考Status Machine Design Pattern;不过我觉得,在你的情况下,状态就这么简单,没必要搞得太复杂
public class Status {
public enum Direction {
SOUTH_WEST((x, y) -> y > 0 && x < 0, "Travelling South-West")
, SOUTH_EAST((x, y) -> y >0 && x > 0, "Travelling South-East")
, NORTH_EAST((x, y) -> x > 0 && y < 0, "Travelling North-East")
, NORTH_WEST((x,y) -> x < 0 && y < 0, "Travelling North-West"), CENTER((x,y) -> x == 0 && y == 0, "");
BiPredicate<Integer, Integer> bp;
String desc;
public BiPredicate<Integer, Integer> getBp() {
return bp;
}
public void setBp(BiPredicate<Integer, Integer> bp) {
this.bp = bp;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
private Direction(BiPredicate<Integer, Integer> bp, String desc) {
this.bp = bp;
this.desc = desc;
}
public static Direction getDirection(int x, int y) {
for (Direction direction : Direction.values()) {
if(direction.getBp().test(x, y)) {
return direction;
}
}
return null;
}
}
public static void main(String[] args) {
Direction d = Direction.getDirection(3, 4);
System.out.println(d.getDesc());
/* if(d == Direction.SOUTH_WEST){
System.out.println("do some thing");
} else if(d == Direction.SOUTH_EAST){
System.out.println("do some thing");
} else if(d == Direction.NORTH_EAST){
System.out.println("do some thing");
} else if(d == Direction.NORTH_WEST){
System.out.println("do some thing");
}*/
}
}