---
title: "三元运算代替if - else结构"
author: "aiyanbo"
date: "2009-08-07T14:35:23Z"
tags: []
slug: "if-else"
draft: false
lastmod: "2009-08-27T23:27:24Z"
id: 442367
url: "https://aiy.iteye.com/blog/442367"
---

String sex="";

if(num==1){
    sex="男";
}else{
    sex="女";
}

换成三元运算 一条代码搞定

String sex=num==1?"男":"女";