英文:
How to increment a value by X
问题
我正在尝试找到一个适用于下面计算的 Java 代码,
找不到如何将值递增 53 和 79,
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("输入重量:");
weight = (int) sc.nextDouble();
b = calculateLandingRate(weight);
System.out.println("总价格:" + 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,
计算应该是值从26kg开始递增53,
/* int rate = +53;
26kg时的总价 = 650 + 53 =703
27kg时的总价 = 703 + 53 =756
28kg时的总价 = 756 + 53 =809
29kg时的总价 = 809 + 53 =862
30kg时的总价 = 862 + 53 =915
*
*
*
75kg时的总价 = 3247 + 53 =3300
*/
}
else if (weight > 75) ;
值递增 79
int rate = +79;
76kg时的总价 = 3300 + 79 =3379
*
*
*
*
624 kg时的总价 = 46592 + 79 =46671
英文:
I'm trying to find a java code to my calculation below,
can't find how to increment the value by 53 and 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) ;`
total price= (the weight 26kg ) + 53 ,
the calculation should be like the value is incremented by 53 ,from the weight =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) ;
the value increment by 79
int rate = +79;
Total price at 76kg = 3300 + 79 =3379
*
*
*
*
Total price at 624 kg = 46592 + 79 =46671
答案1
得分: 2
我认为你想要的是这样的:
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;
}
备注:
- 你的
else if
语句else if (weight > 25 && weight < 75)
总是为假 - 你定义了
rate
但没有使用它
英文:
I think what you are after is this
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;
}
Remarks:
- Your elseif statement
else if (weight> 25 && weight < 75)
is always false - you define
rate
but do not use it
答案2
得分: 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the weight : ");
weight = sc.nextInt();
b = calculateLandingRate(weight);
System.out.println("Total price : " + b);
}
static int calculateLandingRate(int weight) {
final int RATE = 26;
final int LOWERLIMIT = 25;
final int LOWERLIMITADDER = 53;
final int UPPERLIMIT = 75;
final int UPPERLIMITADDER = 79;
// Base price
int price = LOWERLIMIT * RATE;
if (weight > 25 && weight <= 75) {
price += LOWERLIMITADDER * (weight - LOWERLIMIT);
} else if (weight > 75) {
price += LOWERLIMITADDER * (UPPERLIMIT - LOWERLIMIT);
price += UPPERLIMITADDER * (weight - UPPERLIMIT);
}
return price;
}
}
A sample run:
Enter the weight : 24
Total price : 650
Another sample run:
Enter the weight : 25
Total price : 650
Another sample run:
Enter the weight : 40
Total price : 1445
Another sample run:
Enter the weight : 74
Total price : 3247
Another sample run:
Enter the weight : 75
Total price : 3300
Another sample run:
Enter the weight : 80
Total price : 3695
Another sample run:
Enter the weight : 624
Total price : 46671
英文:
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int weight, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the weight : ");
weight = sc.nextInt();
b = calculateLandingRate(weight);
System.out.println("Total price : " + b);
}
static int calculateLandingRate(int weight) {
final int RATE = 26;
final int LOWERLIMIT = 25;
final int LOWERLIMITADDER = 53;
final int UPPERLIMIT = 75;
final int UPPERLIMITADDER = 79;
// Base price
int price = LOWERLIMIT * RATE;
if (weight > 25 && weight <= 75) {
price += LOWERLIMITADDER * (weight - LOWERLIMIT);
} else if (weight > 75) {
price += LOWERLIMITADDER * (UPPERLIMIT - LOWERLIMIT);
price += UPPERLIMITADDER * (weight - UPPERLIMIT);
}
return price;
}
}
A sample run:
Enter the weight : 24
Total price : 650
Another sample run:
Enter the weight : 25
Total price : 650
Another sample run:
Enter the weight : 40
Total price : 1445
Another sample run:
Enter the weight : 74
Total price : 3247
Another sample run:
Enter the weight : 75
Total price : 3300
Another sample run:
Enter the weight : 80
Total price : 3695
Another sample run:
Enter the weight : 624
Total price : 46671
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论