首页 > 解决方案 > 如何将值增加 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 

标签: javacalculatorincrementvar

解决方案


我想你追求的是这个

  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但不使用它

推荐阅读