如何将一个值增加 X。

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

How to increment a value by X

问题

  1. 我正在尝试找到一个适用于下面计算的 Java 代码
  2. 找不到如何将值递增 53 79
  3. public static void main(String[] args) {
  4. int weight, b;
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print("输入重量:");
  7. weight = (int) sc.nextDouble();
  8. b = calculateLandingRate(weight);
  9. System.out.println("总价格:" + b);
  10. }
  11. static int calculateLandingRate(int weight) {
  12. int rate = 26;
  13. if (weight <= 25) {
  14. if (weight > 1) {
  15. int totalPrice = 26 * weight;
  16. } else if (weight > 25 && weight < 75) ;
  17. ------------------------------------------------------------
  18. 总价=重量为26kg+ 53
  19. 计算应该是值从26kg开始递增53
  20. /* int rate = +53;
  21. 26kg时的总价 = 650 + 53 =703
  22. 27kg时的总价 = 703 + 53 =756
  23. 28kg时的总价 = 756 + 53 =809
  24. 29kg时的总价 = 809 + 53 =862
  25. 30kg时的总价 = 862 + 53 =915
  26. *
  27. *
  28. *
  29. 75kg时的总价 = 3247 + 53 =3300
  30. */
  31. }
  32. else if (weight > 75) ;
  33. 值递增 79
  34. int rate = +79;
  35. 76kg时的总价 = 3300 + 79 =3379
  36. *
  37. *
  38. *
  39. *
  40. 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 ,

  1. public static void main(String[] args) {
  2. int weight, b;
  3. Scanner sc = new Scanner(System.in);
  4. System.out.print(&quot;Enter the Weight : &quot;);
  5. weight = (int) sc.nextDouble();
  6. b = calculateLandingRate(weight);
  7. System.out.println(&quot;Total price : &quot; + b);
  8. }
  9. static int calculateLandingRate(int weight) {
  10. int rate = 26;
  11. if (weight&lt;= 25) {
  12. if (weight &gt; 1) {
  13. int totalPrice = 26 * weight;
  14. } 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

  1. /* int rate = +53;
  2. Total price at 26kg = 650 + 53 =703
  3. Total price at 27kg = 703 + 53=756
  4. Total price at 28kg = 756 + 53 =809
  5. Total price at 29kg = 809 + 53 =862
  6. Total price at 30kg = 862 + 53 =915
  7. *
  8. *
  9. *
  10. Total price at 75kg = 3247 + 53 =3300
  11. */
  12. }
  13. else if (weight &gt; 75) ;

the value increment by 79

int rate = +79;

  1. Total price at 76kg = 3300 + 79 =3379
  2. *
  3. *
  4. *
  5. *
  6. Total price at 624 kg = 46592 + 79 =46671

答案1

得分: 2

我认为你想要的是这样的:

  1. static int calculateLandingRate(int weight) {
  2. int lowRate = 26;
  3. int midRate = 53;
  4. int highRate = 79;
  5. int lowRateLimit = 25;
  6. int midRateLimit = 75;
  7. int totalPrice = 0;
  8. if (weight > 1 && weight <= lowRateLimit) {
  9. totalPrice = lowRate * weight;
  10. } else if (weight > lowRateLimit && weight <= midRateLimit) {
  11. totalPrice = lowRate * lowRateLimit + midRate * (weight - lowRateLimit);
  12. } else if (weight > midRateLimit) {
  13. totalPrice = lowRate * lowRateLimit + midRate * (midRateLimit - lowRateLimit) + highRate * (weight - midRateLimit);
  14. }
  15. return totalPrice;
  16. }

备注:

  • 你的 else if 语句 else if (weight > 25 && weight < 75) 总是为假
  • 你定义了 rate 但没有使用它
英文:

I think what you are after is this

  1. static int calculateLandingRate(int weight) {
  2. int lowRate = 26;
  3. int midRate = 53;
  4. int highRate = 79;
  5. int lowRateLimit = 25;
  6. int midRateLimit = 75;
  7. int totalPrice = 0;
  8. if (weight &gt; 1 &amp;&amp; weight &lt;= lowRateLimit) {
  9. totalPrice = lowRate * weight;
  10. } else if (weight &gt; lowRateLimit &amp;&amp; weight &lt;= midRateLimit) {
  11. totalPrice = lowRate * lowRateLimit + midRate * (weight - lowRateLimit);
  12. } else if (weight &gt; midRateLimit) {
  13. totalPrice = lowRate * lowRateLimit + midRate * (midRateLimit - lowRateLimit) + highRate * (weight - midRateLimit);
  14. }
  15. return totalPrice;
  16. }

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

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. int weight, b;
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print("Enter the weight : ");
  7. weight = sc.nextInt();
  8. b = calculateLandingRate(weight);
  9. System.out.println("Total price : " + b);
  10. }
  11. static int calculateLandingRate(int weight) {
  12. final int RATE = 26;
  13. final int LOWERLIMIT = 25;
  14. final int LOWERLIMITADDER = 53;
  15. final int UPPERLIMIT = 75;
  16. final int UPPERLIMITADDER = 79;
  17. // Base price
  18. int price = LOWERLIMIT * RATE;
  19. if (weight > 25 && weight <= 75) {
  20. price += LOWERLIMITADDER * (weight - LOWERLIMIT);
  21. } else if (weight > 75) {
  22. price += LOWERLIMITADDER * (UPPERLIMIT - LOWERLIMIT);
  23. price += UPPERLIMITADDER * (weight - UPPERLIMIT);
  24. }
  25. return price;
  26. }
  27. }

A sample run:

  1. Enter the weight : 24
  2. Total price : 650

Another sample run:

  1. Enter the weight : 25
  2. Total price : 650

Another sample run:

  1. Enter the weight : 40
  2. Total price : 1445

Another sample run:

  1. Enter the weight : 74
  2. Total price : 3247

Another sample run:

  1. Enter the weight : 75
  2. Total price : 3300

Another sample run:

  1. Enter the weight : 80
  2. Total price : 3695

Another sample run:

  1. Enter the weight : 624
  2. Total price : 46671
英文:

Do it as follows:

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. int weight, b;
  5. Scanner sc = new Scanner(System.in);
  6. System.out.print(&quot;Enter the weight : &quot;);
  7. weight = sc.nextInt();
  8. b = calculateLandingRate(weight);
  9. System.out.println(&quot;Total price : &quot; + b);
  10. }
  11. static int calculateLandingRate(int weight) {
  12. final int RATE = 26;
  13. final int LOWERLIMIT = 25;
  14. final int LOWERLIMITADDER = 53;
  15. final int UPPERLIMIT = 75;
  16. final int UPPERLIMITADDER = 79;
  17. // Base price
  18. int price = LOWERLIMIT * RATE;
  19. if (weight &gt; 25 &amp;&amp; weight &lt;= 75) {
  20. price += LOWERLIMITADDER * (weight - LOWERLIMIT);
  21. } else if (weight &gt; 75) {
  22. price += LOWERLIMITADDER * (UPPERLIMIT - LOWERLIMIT);
  23. price += UPPERLIMITADDER * (weight - UPPERLIMIT);
  24. }
  25. return price;
  26. }
  27. }

A sample run:

  1. Enter the weight : 24
  2. Total price : 650

Another sample run:

  1. Enter the weight : 25
  2. Total price : 650

Another sample run:

  1. Enter the weight : 40
  2. Total price : 1445

Another sample run:

  1. Enter the weight : 74
  2. Total price : 3247

Another sample run:

  1. Enter the weight : 75
  2. Total price : 3300

Another sample run:

  1. Enter the weight : 80
  2. Total price : 3695

Another sample run:

  1. Enter the weight : 624
  2. 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:

确定