java - 如何将值增加 X
问题描述
我正在尝试在下面的计算中找到一个 java 代码,
找不到如何将值增加 53 和 79 ,
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Weight : ");
weight = (int) sc.nextDouble();
b = calculateLandingRate(weight);
System.out.println("Total price : " + b);
}
static int calculateLandingRate(int weight) {
int rate = 26;
if (weight<= 25) {
if (weight > 1) {
int totalPrice = 26 * weight;
} else if (weight> 25 && weight < 75) ;`
总价=(重量26kg)+53,计算应该是数值加53,重量=26kg
/* int rate = +53;
Total price at 26kg = 650 + 53 =703
Total price at 27kg = 703 + 53=756
Total price at 28kg = 756 + 53 =809
Total price at 29kg = 809 + 53 =862
Total price at 30kg = 862 + 53 =915
*
*
*
Total price at 75kg = 3247 + 53 =3300
*/
}
else if (weight > 75) ;
值增加 79
积分率 = +79;
Total price at 76kg = 3300 + 79 =3379
*
*
*
*
Total price at 624 kg = 46592 + 79 =46671
解决方案
我想你追求的是这个
static int calculateLandingRate(int weight) {
int lowRate = 26;
int midRate = 53;
int highRate = 79;
int lowRateLimit = 25;
int midRateLimit = 75;
int totalPrice = 0;
if (weight > 1 && weight <= lowRateLimit) {
totalPrice = lowRate * weight;
} else if (weight > lowRateLimit && weight <= midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (weight - lowRateLimit);
} else if (weight > midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (midRateLimit - lowRateLimit) + highRate * (weight - midRateLimit);
}
return totalPrice;
}
评论:
- 你的 elseif 语句
else if (weight> 25 && weight < 75)
总是错误的 - 你定义
rate
但不使用它
推荐阅读
- mysql - MYSQL 5.7 注意 读取通信包时出错
- google-apps-script - 谷歌幻灯片 - 如何自动运行谷歌插件并应用自定义布局?
- json - 如何从 postlist(posts.js) 中获取 Id 并在 post.js 页面上以反应原生方式显示
- python-3.x - 在python3中过滤和转置数据帧
- javascript - 浏览器阻止了 javascripts
- mysql - 如何修复:“错误:需要数据和哈希参数”
- angular - 如何防止在角度 6 中评估 ngIf 条件的延迟?在评估值之前显示默认图像。如何防止这种情况?
- r - 有没有办法在 R 中使用 ggplot 在地图的两个区域之间添加空间?
- python - 使用 setattr() 时获取真正的调用方方法名称
- jenkins - pollSCM 触发器在这个 Jenkinsfile 中指的是什么?