C ++如果没有if函数的问题怎么解决? [英] C++ how do I solve if problems without the if function?

查看:103
本文介绍了C ++如果没有if函数的问题怎么解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个赋值,我不能在C ++中使用if()或if else语句。



问题在于:

如果输入的数字大于2.375,则设为= 2.375

如果数字小于0,则将其设为=。



如果没有函数,我该怎么做?我只是新手学习C ++:3



我尝试过:



到目前为止,我还没有想到我能学到的任何可以做到的事情。任何帮助将不胜感激!

解决方案

您的朋友是 三元运营商 [ ^ ]!



/ ravi


几种方式:

1)三元组运算符:

  value  =  value > ;  0  value  0 ; 

但......从技术上讲,这是一个简短形式 if ... else ...

2)作弊,并在 >:

  while  value < ;  0  value  =  0 ; 

这不是一个有效的解决方案,但是......它有效。

3)作弊效率更低,使用

  for (;  value < ;  0 ;) value  =  0 ; 


Basically I have an assignment where I can't use if() or if else statements in C++.

The problem is saying :
If the entered number is greater than 2.375, make it = 2.375
If the number is less than 0, make it =0.

How do I do this without if functions? I'm just newbie learning C++ :3

What I have tried:

I so far cannot think of anything I've learned yet that can do this. Any help would be appreciated!

解决方案

Your friend is the ternary operator[^]!

/ravi


Couple of ways:
1) The ternary operator:

value = value > 0 ? value : 0;

But ... technically, that;s a short form of if ... else ...
2) Cheat, and use a while:

while (value < 0) value = 0;

It's not an efficient solution, but ... it works.
3) Cheat even less efficiently and use a for:

for ( ; value < 0; ) value = 0;


这篇关于C ++如果没有if函数的问题怎么解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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