java - 布尔变量
问题描述
我以为布尔的默认值是假的?为什么它会打印真实的陈述呢?我的输出是再见
public class Test {
public static void main (String [] args) {
if(false)
System.out.print("hello");
else System.out.print("goodbye");
}
}
解决方案
您的代码不使用布尔值的默认值。你总是 print System.out.print("goodbye");
,因为这部分是真的。为此,请使用以下代码
public class Test {
static boolean defaultValue;
public static void main(String[] args) {
System.out.println("Default value is "+defaultValue);
if(defaultValue)
System.out.println("hello");
else
System.out.println("goodbye");
}
}
推荐阅读
- c++ - 如何将 USB 存储设备路径映射到物理驱动器?
- android - 如何删除“Google Play Protect”警告?
- postgresql - 如何在插入显示测试消息之前创建触发器功能
- c# - 如何通过选中复选框添加值并在取消选中复选框时删除值
- sql - 如何修复 MariaDB 的 MS SQL 链接服务器缓慢问题
- php - 带有 MSSQL 的 PHP 原则:':' 附近的语法不正确
- android - 在回收站视图项目中,单个图像视图上的多个 url 加载
- python-3.x - 如何使用 python 在暗网中运行预训练的权重?
- laravel - 我正在使用 Putty 在 Godaddy VPS 上运行和编译 vue js + laravel 应用程序。更改未生效
- php - mysql 将列更新为 null 或其值