java - java中的if语句+计算总数
问题描述
当我用java编写这个例子时,我遇到了一个问题:
if ( deadline==1){
total = A + B + C;
System.out.println(" you get =" + total);
}
if ( deadline==2 ){
total = A + B + C;
Punishment = total*(5\100);
Final = total - Punishment;
System.out.println(" you get =" + Final);
}
当我运行它时,我在黑屏上写了最后期限=2
,但它显示在黑屏上(你得到=总计)
要清楚我有(A,B,C)的值。我在if
声明之前写 + 我之前要求用户使用Scanner
.
解决方案
在 Java 中,如果一个运算的两个操作数都是整数,则该运算作为整数运算完成。
您的代码正在执行Punishment = total*(5\100)
- 这里除法是作为整数运算完成的,也就是说,结果(5*100)
是0
(零),因此Punishment
也将是零。
选项1:将其更改Punishment = (total * 5) \ 100
为首先完成乘法(不需要括号)。
选项2:使用双重喜欢Punishment = (int)(total * (5.0\100))
选项 3:也将变量更改为双精度:
double punishment;
...
punishment = total * 5.0 / 100.0;
注意:变量名按约定应以小写字母开头
假设deadline
是2
推荐阅读
- python - 如何为具有多个通用外键的模型创建工厂?
- google-apps-script - 如何根据 googlescript 中的条件抛出错误?
- prolog - Prolog 路径回溯根据网格大小永远运行
- android - Android - 设备 ID 的唯一性如何?
- wordpress - 博客文章前端未显示链接和中断
- ionic4 - 无法在 Ionic 4 中创建新项目
- javascript - 反应地图运算符
- arrays - Powershell 将数组中的数据组合成一个对象
- python - 图片 PIL -“ValueError:波段索引超出范围”
- couchbase - Couchbase 5.5:为什么将 '\\' 放入 meta.id 会使搜索更快?