java - 使用 if 找出三个数中最小的数
问题描述
所以我很困惑那些 if 括号中的内容,代码工作正常但我不明白?
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if(b < a) {
a = b; //This part is confusing to me
}
if(c < a) {
c = a;
}
System.out.println("Smallest number is " + a);
}
解决方案
a
用于两个目的:
- 它正在存储第一个输入。
- 如果该新值较小,则将使用新值(来自其他变量,从输入中获取其值)对其进行更新。
如果我正在编写程序,我不会将这两个职责都指定给a
,但这就是这里发生的事情。
推荐阅读
- php - 无法获取表单以插入数据
- computer-vision - 为什么平移是本质矩阵的空向量
- c# - 如何获取对象的副本
- python - 使用python将子列表中的第一个元素与同一列表中的元素合并
- python - NoReverseMatch at /post/new Reverse for 'post_detail' 未找到。“post_detail”不是有效的视图函数或模式名称
- php - 无法在Mysql数据库中删除或插入数据
- asp.net-core - 在 ASP.NET Core 应用程序中为 HTTPS 自定义端口
- java - 升级 android studio 后“无法解决:support-v4”
- macos - 从 macbooks 中删除自动完成 url 建议
- java - 按集合中的内部集合元素类型分组