用户:
Godeyes查看:9 回复:6 评论:9 创建时间:2020-05-07T07:45:39
哈喽大家好,我是勤快更新的萌新------Godeyes,今天我们继续学习Java


在 Java 语言中布尔类型的值只包括 true 和 false,没有其他值,不包括 1 和 0,布尔类型的数据在开发中主要使用在逻辑判断方面,例如:如果外面在下雨,我出门带一把雨伞。如果明天休息,咱们就一起出去玩耍吧。请看一段程序(以下程序中使用了控制语句,后面会详细讲,
先大概了解一下):
public class BooleanTest1 {
public static void main(String[] args) {
boolean isRain = true;
if(isRain){
System.out.println("外面下雨了,出门要带一把雨伞哦!");
}else{
System.out.println("外面天气晴朗,走起吧!");
}
boolean 喵 = true;
if(喵){
System.out.println("哥们你好");
}else{
System.out.println("姐们你好");
}
}
}
我们对以上程序进行编译并运行,请看下图运行结果:
接下来对以上程序进行一个简单的解释:其中第 3 行代码表示定义一个布尔类型的变量isRain 来表示是否下雨了,给其赋值 true,以下的判断逻辑是如果 isRain 为 true 则输出"外面下雨了,出门要带一把雨伞哦!",反之则输出"外面天气晴朗,走起吧!"。第 9 行代码表示定义一个布尔类型的变量 喵 来表示性别,判断逻辑是如果 喵 为 true 则输出"哥们你好",反之则输出"姐们你好"。
接下来,我们再来看一段代码,布尔类型变量的值是否可以使用 1 和0:
public class BooleanTest2 {
public static void main(String[] args) {
boolean flag = 1;
boolean success = 0;
}
}
通过以上的测试结果可以看出,在 Java 中布尔类型的变量值不能使用 1 和 0,只能使用 true和 false。
通过本小节的学习,大家需要掌握的是在 Java 语言中 boolean 类型的数据只有两个值,分别是 true 和 false,没有其他值,并且 boolean 类型在开发中主要使用在逻辑判断方面。
GodeyesJava学习教程(第一章)[介绍java]:https://shequ.codemao.cn/community/281707
java学习教程(第二章)[java的注释]:https://shequ.codemao.cn/community/281722
Java(第三章)[编写第一个java程序]:https://shequ.codemao.cn/community/281753
Java(特殊篇)[运行和编译java程序]:https://shequ.codemao.cn/community/281785
Java学习教程(第四章)[标识符与关键字]:https://shequ.codemao.cn/community/282020
Java学习教程(第五章)[变量]{1.字面量}:https://shequ.codemao.cn/community/282531
Java学习教程(第五章)[变量]{变量概述}:https://shequ.codemao.cn/community/283059
Java学习教程(第五章)[变量]{使用变量}:https://shequ.codemao.cn/community/283194
[教程帖]Java学习教程第五章[变量]{变量分类}:https://shequ.codemao.cn/community/283732
[教程帖]Java学习教程(第五章)[变量]{变量作用域和章节总结}:https://shequ.codemao.cn/community/283745
[教程帖]Java学习教程(第六章)[数据类型]{数据类型概述}:https://shequ.codemao.cn/community/284512
[教程帖]Java学习教程(第六章)[数据类型]{字符编码}:https://shequ.codemao.cn/community/285286
[教程帖]Java学习教程(第六章)[数据类型]{字符型详解 理解}:https://shequ.codemao.cn/community/286136
[教程帖]Java学习教程(第六章)[数据类型]{整数型详解(理解)}:https://shequ.codemao.cn/community/289851
[教程帖]Java学习教程(第六章)[数据类型]{布尔型详解(理解)}:https://shequ.codemao.cn/community/290606
点赞0
评论