Java:将用户输入的变量从一种方法调用到另一种方法

时间:2015-02-10 03:40:11

标签: java methods quadratic

我目前正在修改以前的Java程序,该程序通过将部分代码分解为方法并调用这些方法来完成相同的任务来计算二次公式类型的数学问题。目前,我一直在创建一种计算分子中判别式的方法。按照指定,我应该有一个方法接收a,b和c值的用户输入,但是我不确定如何将这些值从一个方法中获取到下一个应该是在计算中使用这些值。

我的导师希望我们将a和b变量输入到数组中,我知道它现在的方式是将值放入数组的一种非常手动的方式,但是仍然可以用于此目的。

这是我到目前为止所感谢的阅读。

编辑 :我已经从头开始了,我无法弄清楚如何正确地从我的方法中返回信息,以便下一个可以用它。我不断得到方法参数不适用的错误。有什么想法吗?

import java.util.*;

public class QuadraticMethods {
public static void main(String[] args){

    getValues();
    calcDisc(userInput);



}


public static double[] getValues() {
    double[] userInput;
    userInput = new double[3];
    Scanner kbd = new Scanner(System.in);
    System.out.println("Fourth Assignment by MyNameHere");
    System.out.println("Welcome to the quadratic formula computation tool.");
    System.out.println("This tool will solve problems in the form of: a^x + bx + c.");
    System.out.println("Please enter the values you would like for a, b, and c.");
    for (int i = 0; i < userInput.length; i++) {
        userInput[i] = kbd.nextDouble(); }


    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    /*
    System.out.println(aValue);
    System.out.println(bValue);
    System.out.println(cValue);
     */
    return userInput;
}


public static double calcDisc(double[] userInput) {
    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    double radicalValue = (Math.pow(bValue, 2) - (4*aValue*cValue));
    System.out.println(radicalValue);
    return radicalValue;
}

}

1 个答案:

答案 0 :(得分:1)

要使您当前的代码正常工作,只需要进行一些小改动:

public static void main(String[] args) {

    double[] userInput = getValues();
    calcDisc(userInput);
}

此外,实际上并未使用这些作业。

public static double[] getValues() {
    // ...

    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];

    // ...
}

其他一些改进可能是:

结果不应由计算结果的方法打印。您已经通过返回值以正确的方式声明了该方法。现在您应该使用返回的值并在调用方法中打印结果。

public static void main(String[] args) {

    double[] userInput = getValues();
    double radicalValue = calcDisc(userInput);

    System.out.println(radicalValue);
}

// ...

    public static double calcDisc(double[] userInput) {
    double aValue = userInput[0];
    double bValue = userInput[1];
    double cValue = userInput[2];
    double radicalValue = (Math.pow(bValue, 2) - (4 * aValue * cValue));
    return radicalValue;
}

打印横幅可能不应与请求用户输入混合。想象一下,您需要重复读取/评估/打印周期:

public static void main(String[] args) {

    while (true) {
        double[] userInput = getValues();
        double radicalValue = calcDisc(userInput);

        System.out.println(radicalValue);
    }
}

每次都会打印横幅文字。隔离职责使您可以在不影响不相关代码的情况下改变行为。

public static void main(String[] args) {

    printBanner();
    while (true) {
        double[] userInput = getValues();
        double radicalValue = calcDisc(userInput);

        System.out.println(radicalValue);
    }
}

private static void printBanner() {
    System.out.println("Fourth Assignment by MyNameHere");
    System.out.println("Welcome to the quadratic formula computation tool.");
    System.out.println("This tool will solve problems in the form of: a^x + bx + c.");
}

扫描仪应在使用后关闭。 Java 7尝试使用资源将为您做到这一点。

public static double[] getValues() {
    double[] userInput;
    userInput = new double[3];
    try (Scanner kbd = new Scanner(System.in)) {
        System.out.println("Please enter the values you would like for a, b, and c.");
        for (int i = 0; i < userInput.length; i++) {
            userInput[i] = kbd.nextDouble();
        }
    }
    return userInput;
}
相关问题