如何在函数参数中使用全局变量?

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

How do use Global varibales in a function argument?

问题

问题在于我无法在参数中使用我的全局变量(a、b、c)。我需要在我的布尔函数和双精度函数中使用它们。我做错了什么?我该如何修复?

public class triareamain extends javax.swing.JFrame {

    double a, b, c;

    public void DisplayError() {
        side1input.setText("Error");
        side2input.setText("Type");
        side3input.setText("+ Integers");
    }

    public double areaCal(double a, double b, double c) {
        double s = (a + b + c) / 2;
        double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        return area;
    }

    public static boolean isValid(double a, double b, double c) {
        if (a > b + c || b > a + c || c > a + b) {
            return true;
        } else {
            return false;
        }
    }

    private void calculatebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                
        try {
            a = Double.valueOf(side1input.getText());
            b = Double.valueOf(side2input.getText());
            c = Double.valueOf(side3input.getText());

            boolean area = isValid();

            if (area == false) {
                double finalarea = areaCal();
            } else {
                DisplayError();
            }

        } catch (NumberFormatException e) {
            side1input.setText("Error");
            side2input.setText("Type");
            side3input.setText("+ Integers");
        }
    }
}
英文:

So my issue is that I cannot use my global variables (a,b,c) in my arguments. I need to be able to use them in my boolean function and double function. What am I doing wrong? How can I fix this?

 public class triareamain extends javax.swing.JFrame {
double a, b, c; 
public void DisplayError() {
side1input.setText("Error");
side2input.setText("Type");
side3input.setText("+ Integers");
}
public double areaCal(double a, double b, double c) {
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
public static boolean isValid(double a, double b, double c) {
if (a > b + c || b > a + c || c > a + b) {
return true;
} else {
return false;
}
}
private void calculatebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                
try {
a = Double.valueOf(side1input.getText());
b = Double.valueOf(side2input.getText());
c = Double.valueOf(side3input.getText());
boolean area = isValid();
if (area == false) {
double finalarea = areaCal();
} else {
DisplayError();
}
} catch (NumberFormatException e) {
side1input.setText("Error");
side2input.setText("Type");
side3input.setText("+ Integers");
}

答案1

得分: 1

abc 不是全局变量。Java 没有这个概念。它们是 triareamain 类的字段

但是你还创建了同名的参数,所以这些名字正在遮蔽这些字段。

如果你希望 areaCal() 方法直接使用这些字段,移除参数:

public double areaCal() {

如果你希望该方法使用参数,那么在调用时传入值:

double finalarea = areaCal(a, b, c);

如果保留参数,我强烈建议你要么重命名字段,要么重命名参数。变量名的遮蔽对程序员来说是非常令人困惑的,极有可能引起 bug。

英文:

a, b, and c are not global variables. Java doesn't have that concept. They are fields of class triareamain.

However you also created parameters of the same name, so those names are shadowing the fields.

If you wanted the areaCal() method to use the fields directly, remove the parameters:

public double areaCal() {

If you want the method to use the parameters, then pass values in the call:

double finalarea = areaCal(a, b, c);

If you keep the parameters, I highly recommend that you rename either the fields or the parameters. Shadowing of variable names is very confusing to the programmer, and will in high probability be the cause of bugs.

huangapple
  • 本文由 发表于 2020年4月10日 07:28:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61131827.html
匿名

发表评论

匿名网友

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

确定