---
title: "复合三元运算代替多重if-else结构"
author: "aiyanbo"
date: "2009-08-08T21:59:03Z"
tags: []
slug: "if-else"
draft: false
lastmod: "2009-08-27T23:27:41Z"
id: 443058
url: "https://aiy.iteye.com/blog/443058"
---

String state="";

if(num==1){
    state="自动";
}else if(num==2){
    state="半自动";
}else if(num==3){
    state="手动";
}

用复合三元运算 如下：

String state=num==3?"手动":(num==1?"自动":"半自动");

由一段代码变成一条代码，结构清晰，一目了然