Java 有没有办法重构这些if语句?
只是想知道是否有办法重构下面的代码?我是Java新手,正在尝试编写枯燥的代码——下面我已经编写了,但似乎有很多条件需要检查Java 有没有办法重构这些if语句?,java,if-statement,conditional-statements,refactoring,Java,If Statement,Conditional Statements,Refactoring,只是想知道是否有办法重构下面的代码?我是Java新手,正在尝试编写枯燥的代码——下面我已经编写了,但似乎有很多条件需要检查 void printDirection() { if (yDirection > 0) { if (xDirection < 0) { println("Travelling South-West"); } else { println("Travelling South-East"); } } else
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");
}
}
}
void printDirection(){
如果(Y方向>0){
if(xDirection<0){
println(“西南旅行”);
}否则{
println(“东南行”);
}
}else if(Y方向<0){
如果(xDirection,您可以分别评估北/南和东/西条件,并将方向粘贴到消息中
System.out.printf("Travelling %s-%s%n", (yDirection < 0 ? "North" : "South"),
(xDirection < 0 ? "West" : "East"));
System.out.printf(“移动%s-%s%n”,(y方向<0?“北”:“南”),
(X方向<0?“西”:“东”);
根据你问题中的代码,我假设你只关心这四个互补方向(不是正北、正东、静止等).如果你真的想让它变干,可以使用操作符来完成,但它既不容易阅读,也不推荐。它被用于编程竞赛中,目标是尽可能快地进行
计划如下:
(条件?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语句,请参考状态机设计模式;但我认为,在您的情况下,状态是如此简单,不需要使其太复杂
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");
}*/
}
}
公共类状态{
公共枚举方向{
西南((x,y)->y>0&&x<0,“西南行”)
,东南((x,y)->y>0&&x>0,“向东南移动”)
,东北((x,y)->x>0和&y<0,“向东北移动”)
,西北((x,y)->x<0&&y<0,“向西北移动”),中心((x,y)->x==0&&y==0,”;
双预测bp;
字符串描述;
公共双信用getBp(){
返回bp;
}
公共无效收单点(双预测收单点){
这个.bp=bp;
}
公共字符串getDesc(){
返回描述;
}
公共void setDesc(字符串desc){
this.desc=desc;
}
专用方向(双预测bp,字符串描述){
这个.bp=bp;
this.desc=desc;
}
公共静态方向getDirection(整数x,整数y){
对于(方向:Direction.values()){
if(direction.getBp().test(x,y)){
返回方向;
}
}
返回null;
}
}
公共静态void main(字符串[]args){
方向d=方向.getDirection(3,4);
System.out.println(d.getDesc());
/*if(d==西南方向){
System.out.println(“做点什么”);
}else if(d==东南方向){
System.out.println(“做点什么”);
}else if(d==北东方向){
System.out.println(“做点什么”);
}else if(d==西北方向){
System.out.println(“做点什么”);
}*/
}
}
您可以将北/南
部分和东/西
部分分配给两个字符串,然后打印包含这两个字符串的消息。更好的做法是,可以使用枚举对北、南、西、东
进行编码。啊,已经有人这样回答了,但我很感激有人解释为什么会这样做这样编码!谢谢:)