有没有简化Java中的if…else语句的方法

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

Is there a way to simplify if..else statements in Java

问题

这是检查体育项目ID并根据体育项目设置图像的if语句。但我想知道是否有一种更简单和更短的方法来实现这个。感谢您的时间和答案!

if (sport.equals("1")) {
    sportImg.setImageResource(R.drawable.fudbal);
} else if (sport.equals("2")) {
    sportImg.setImageResource(R.drawable.hokej);
} else if (sport.equals("3")) {
    sportImg.setImageResource(R.drawable.basketball);
} else if (sport.equals("4")) {
    sportImg.setImageResource(R.drawable.tenis);
} else if (sport.equals("5")) {
    // sportImg.setImageResource(R.drawable.handball);
} else if (sport.equals("6")) {
    // sportImg.setImageResource(R.drawable.mlb);
} else if (sport.equals("7")) {
    sportImg.setImageResource(R.drawable.odbojka);
} else if (sport.equals("8")) {
    sportImg.setImageResource(R.drawable.ragbi);
} else if (sport.equals("9")) {
    // sportImg.setImageResource(R.drawable.formula);
} else if (sport.equals("10")) {
    // sportImg.setImageResource(R.drawable.moto);
} else if (sport.equals("11")) {
    sportImg.setImageResource(R.drawable.vaterpolo);
} else if (sport.equals("12")) {
    // sportImg.setImageResource(R.drawable.boks);
} else if (sport.equals("13")) {
    sportImg.setImageResource(R.drawable.futsal);
} else if (sport.equals("14")) {
    // sportImg.setImageResource(R.drawable.tenis);
} else {
    sportImg.setImageAlpha(0);
}
英文:

I have this if statements that check the ID of a sport and set an Image according to the sport.
but I am curious if there is a more simple and shorter way to implement this.
Thank you for the time and answers!

if (sport.equals("1")) {
    sportImg.setImageResource(R.drawable.fudbal);
} else if (sport.equals("2")) {
    sportImg.setImageResource(R.drawable.hokej);
} else if (sport.equals("3")) {
    sportImg.setImageResource(R.drawable.basketball);
} else if (sport.equals("4")) {
    sportImg.setImageResource(R.drawable.tenis);
} else if (sport.equals("5")) {
    // sportImg.setImageResource(R.drawable.handball);
} else if (sport.equals("6")) {
    // sportImg.setImageResource(R.drawable.mlb);
} else if (sport.equals("7")) {
    sportImg.setImageResource(R.drawable.odbojka);
} else if (sport.equals("8")) {
    sportImg.setImageResource(R.drawable.ragbi);
} else if (sport.equals("9")) {
    // sportImg.setImageResource(R.drawable.formula);
} else if (sport.equals("10")) {
    // sportImg.setImageResource(R.drawable.moto);
} else if (sport.equals("11")) {
    sportImg.setImageResource(R.drawable.vaterpolo);
} else if (sport.equals("12")) {
    // sportImg.setImageResource(R.drawable.boks);
} else if (sport.equals("13")) {
    sportImg.setImageResource(R.drawable.futsal);
} else if (sport.equals("14")) {
    // sportImg.setImageResource(R.drawable.tenis);
} else {
    sportImg.setImageAlpha(0);
}

答案1

得分: 1

在这种情况下,您可以使用Map<String, Integer>来简化if..else语句:

Map<String, Integer> mapImages = new HashMap<>();

mapImages.put("1", R.drawable.fudbal);
mapImages.put("2", R.drawable.hokej);
// mapImages.put(...

Integer imageId = mapImages.get(sport);
if (imageId != null) {
    sportImg.setImageResource(imageId);
} else {
    sportImg.setImageAlpha(0);
}

或者您可以使用Map<String, Runnable>来为每个key执行一些操作:

Map<String, Runnable> mapActions = new HashMap<>();

mapActions.put("1", () -> System.out.println("Action 1"));
mapActions.put("2", () -> System.out.println("Action 2"));

Runnable defaultAction = () -> System.out.println("Default action");

mapActions.getOrDefault("1", defaultAction).run(); // Action 1
mapActions.getOrDefault("2", defaultAction).run(); // Action 2
mapActions.getOrDefault("3", defaultAction).run(); // Default action

或者您可以在普通的Java switch语句中使用字符串

String typeOfDay;
switch (dayOfWeekArg) {
    case "Monday":
        typeOfDay = "工作周的开始";
        break;
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
        typeOfDay = "中间的一天";
        break;
    case "Friday":
        typeOfDay = "工作周的结束";
        break;
    case "Saturday":
    case "Sunday":
        typeOfDay = "周末";
        break;
    default:
        throw new Exception("无效的星期几: " + dayOfWeekArg);
}
英文:

In this case you can use Map<String, Integer> to simplify if..else statements:

Map<String, Integer> mapImages = new HashMap<>();

mapImages.put("1", R.drawable.fudbal);
mapImages.put("2", R.drawable.hokej);
// mapImages.put(...

Integer imageId = mapImages.get(sport);
if (imageId != null) {
    sportImg.setImageResource(imageId);
} else {
    sportImg.setImageAlpha(0);
}

Or you can use Map<String, Runnable> to do some stuff for each key:

Map<String, Runnable> mapActions = new HashMap<>();

mapActions.put("1", () -> System.out.println("Action 1"));
mapActions.put("2", () -> System.out.println("Action 2"));

Runnable defaultAction = () -> System.out.println("Default action");

mapActions.getOrDefault("1", defaultAction).run(); // Action 1
mapActions.getOrDefault("2", defaultAction).run(); // Action 2
mapActions.getOrDefault("3", defaultAction).run(); // Default action

Or you can use plain old Java strings in switch statements:

String typeOfDay;
switch (dayOfWeekArg) {
    case "Monday":
        typeOfDay = "Start of work week";
        break;
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
        typeOfDay = "Midweek";
        break;
    case "Friday":
        typeOfDay = "End of work week";
        break;
    case "Saturday":
    case "Sunday":
        typeOfDay = "Weekend";
        break;
    default:
        throw new Exception("Invalid day of the week: " + dayOfWeekArg);
}

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

发表评论

匿名网友

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

确定