如何在java中使用具有布尔数据类型的用户性别输入? [英] How can I use a user's gender input with a boolean data type in java?

查看:1121
本文介绍了如何在java中使用具有布尔数据类型的用户性别输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的计算机科学课写一个程序,我必须提示用户他们的性别。该计划计算他们的BMR,他们是两个等式:男性为1,女性为1。我想使用用户的输入来决定程序需要执行哪个等式。

解决方案

让我向你介绍运营商 [ ^ ] ...这对你需要的转换非常有用...

因为你只有两个有效值(M和F)你可以正确地将它转换为逻辑(布尔)是或没有。你所要做的就是提出正确的问题...

你问的原始问题是这个人是女性吗?。这个问题当然无法回答是或否,所以让我再提一个问题:这个人是男性吗?。现在这是一个问题,您只需回答是或否,但是它是否涵盖所有选项?是的,因为你在应用程序中只有两个选项,任何答案都会产生明确的答案...

使用Java的三元运算符可以这样写:

< pre lang =java> boolean bIsMale =(szInput == M)? true:false;


I'm writing a program for my computer science class and I have to prompt the user for their gender. the program calculates their BMR and their are two equation: 1 for men and 1 for women. I want to use the user's input to decide which equation the program needs to execute.

解决方案

Let me introduce you the ternary operator[^]...it very useful for such converting you need here...
As you have only two valid values (M and F) you rightly can turn it to logic (boolean) yes or no. All you have to do is ask the right question...
The original question you asked is 'Is this person male of female?'. Such question of course can not be answered with yes or no, so let me suggest you an other question: 'Is this person a male?'. Now that's a question you can simply answer with yes or no, but does it cover all the options? Yes, as you have only two option in you application any answer yields a definite answer...
Using the ternary operator of Java i t can be written like this:

boolean bIsMale = (szInput == "M") ? true : false;


这篇关于如何在java中使用具有布尔数据类型的用户性别输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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