什么是“?”用Java表示? [英] What does "?" mean in Java?

查看:151
本文介绍了什么是“?”用Java表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道问号()在java中代表什么,我正在做一个小程序,一个Nim游戏。正在寻找一本书,寻求帮助并看到这句话:

I dont know what the question mark (?) stand for in java, I was doing a small program, a Nim-game. were looking in a book, for help and saw this statement:

int pinsToTake = (min >= 2) ? 2 : 1;

我不明白,会是什么?表示,它可以与if语句有关但你把它放在一个变量中吗?并且可以是其他的东西? (我刚才说的这些东西可能会产生误导)

I don't understand it, what will ? represent, can it be something to do with if-statement but you put it in a variable? and the : can be something "else"? (this things that I just said can be very misleading)

推荐答案


someval =(min> = 2)? 2:1;

someval = (min >= 2) ? 2 : 1;

这称为三元运算符,可用作 if-else 。这相当于

This is called ternary operator, which can be used as if-else. this is equivalent to

if((min >= 2) {
   someval =2;
} else {
   someval =1
}

关注教程了解更多信息和用法。

Follow this tutorial for more info and usage.

这篇关于什么是“?”用Java表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆