如何将一个值增加 X。

huangapple go评论77阅读模式
英文:

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(&quot;Enter the Weight : &quot;);
weight = (int) sc.nextDouble();
b = calculateLandingRate(weight);
System.out.println(&quot;Total price  : &quot; + b);
}
static int calculateLandingRate(int weight) {
int rate = 26;
if (weight&lt;= 25) {
if (weight &gt; 1) {
int totalPrice = 26 * weight;
} else if (weight&gt; 25 &amp;&amp; weight &lt; 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 &gt; 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 &gt; 1 &amp;&amp; weight &lt;= lowRateLimit) {
totalPrice = lowRate * weight;
} else if (weight &gt; lowRateLimit &amp;&amp; weight &lt;= midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (weight - lowRateLimit);
} else if (weight &gt; midRateLimit) {
totalPrice = lowRate * lowRateLimit + midRate * (midRateLimit - lowRateLimit) + highRate * (weight - midRateLimit);
}
return totalPrice;
}

Remarks:

  • Your elseif statement else if (weight&gt; 25 &amp;&amp; weight &lt; 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(&quot;Enter the weight : &quot;);
weight = sc.nextInt();
b = calculateLandingRate(weight);
System.out.println(&quot;Total price  : &quot; + 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 &gt; 25 &amp;&amp; weight &lt;= 75) {
price += LOWERLIMITADDER * (weight - LOWERLIMIT);
} else if (weight &gt; 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

huangapple
  • 本文由 发表于 2020年5月4日 02:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/61579702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定