如何使用我创建的方法?爪哇 [英] How do I use a method I have made? JAVA

查看:60
本文介绍了如何使用我创建的方法?爪哇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个方法来使用函数 b^2-4ac 找到判别式

I have created this method to find the discrimiant using the function b^2-4ac

public class Discriminant {
public double Disc (double a, double b, double c){
        double disc;
        disc = Math.pow(b, 2) - 4*a*c;
        return disc ;
    }


}

然后我做了另一种方法来找到一个函数的所有正根

Then i made another method to find all positive roots of a function

public class PlusRoot {

    public double plusRoot (double a, double b, double c){
        double proot;
        proot = -b + Math.sqrt(disc)/ 2*a;
        return proot;
    }
}

但是它不起作用,因为它说不能将光盘解析为变量,两种方法都在同一个包中...我该如何解决这个问题?

But it is not working because it says disc cannot be resolved as a variable, both methods are in the same package... how do I fix this?

推荐答案

错误的原因是你没有变量disc.您需要在使用之前对其进行初始化.你需要像

The reason for the error is that you do not have the variable disc. You need to initialize it before using it. You would need a line like

double disc = 0.0;

但是,我也猜测您的意图是使用您的 Disc 方法的返回值.你的两个班级现在是如何设置的.在使用变量 disc 之前,您必须实例化一个 Discriminant 对象,然后像这样调用 Disc 方法.

However, I am also guessing your intention was to use the return value of your Disc method. With how your two classes are set up now. You would have to instantiate a Discriminant object and then call the method Disc method like so before you use the variable disc.

Discriminant discriminant = new Discriminant();
double disc = discriminant.Disc(a, b, c); //or whatever 3 doubles you wish to use.

查看 java 中的不同作用域变量以更好地理解这个问题.

Look into different scope variables in java to better understand this issue.

这篇关于如何使用我创建的方法?爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆