简体   繁体   English

代码行上的StackOverFlow错误

[英]StackOverFlow Error on line of code

I am taking a programming class and set up code about computing a paycheck. 我正在上编程班,并设置有关计算薪水的代码。 Everything works fine except for line 11. I end up getting a stackoverflow error. 除第11行外,其他所有程序都正常运行。我最终遇到了stackoverflow错误。

However when I remove this line of code 但是,当我删除这一行代码时

double weeksWages = pay(50, 10); // weeksWages is 550

The error goes away, but when running program, I end up with 10 instead of 550 that is intended. 错误消失了,但是运行程序时,我得到的是10而不是预期的550。 This is probably really simple to fix, but not sure. 这可能真的很容易解决,但不确定。 Thanks! 谢谢!

Here is the full code: 这是完整的代码:

import java.util.Scanner;
import static java.lang.System.out;

public class ComputePayCheck {    

  static Scanner in = new Scanner(System.in);


  public static double pay(int hours, double hourlyRate) {
     int otHours = (hours > 40) ? hours - 40 : 0;
     double weeksWages = pay(50, 10); // weeksWages is 550
     return otHours;
  }

  public static void main(String[] args) {
     out.print("Enter hours worked: ");
     int hours = in.nextInt();
     out.print("Enter hourly rate: ");
     double hourlyRate = in.nextDouble();
     out.print("Week's Salary is: " + pay(hours, hourlyRate));


  }

}

You're getting a stack overflow because your pay function is recursively calling itself with no end in sight. 之所以会出现堆栈溢出,是因为您的pay函数正在递归地调用自身而没有尽头。 I'm not sure what the exact expected behaviour of your method is, but try something like this instead. 我不确定您的方法的确切预期行为是什么,但是尝试这样的方法。

public static double pay(int hours, double hourlyRate) {
 int otHours = (hours > 40) ? hours - 40 : 0;
 return hourlyRate * otHours;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM