【问题标题】:Why does this function not working? I am new in java为什么这个功能不起作用?我是java新手
【发布时间】:2023-02-16 23:06:32
【问题描述】:
public class Roots{
    public static void main(String[] args) {
        
        
    }       
        
    public void roots(int coefSquare, int coefX, int noCoef) {
        
        int square1 = (int) (((coefX*(-1)) + Math.sqrt(coefX*coefX - 4*coefSquare*noCoef))/2*coefSquare);
        int square2 = (int) (((coefX*(-1)) - Math.sqrt(coefX*coefX - 4*coefSquare*noCoef))/2*coefSquare);
        roots(1 , 6 , 9);
        System.out.println(square1);
        System.out.println(square2);
    }   
}

我试图把这个功能从空白中剔除。它没有用。

【问题讨论】:

  • 您没有调用该函数。
  • 除了您不调用函数/方法之外,该方法还包含对自身的无条件调用 roots(1 , 6 , 9); 这将导致该方法调用自身,直到发生堆栈溢出(如果曾经调用过)。您可能是想将该行放入您的主要方法中吗?
  • 欢迎来到堆栈溢出!这是您开始熟悉using a debugger 的好机会。当您在调试器中单步执行代码时,哪个操作首先会产生意外结果?该操作中使用的值是什么?结果如何?预期的结果是什么?为什么?要了解有关此社区的更多信息以及我们如何为您提供帮助,请从tour 开始并阅读How to Ask 及其链接资源。

标签: java function square


【解决方案1】:

在 main 方法中调用您的函数并将 roots 函数设为静态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多