繁体   English   中英

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

[英]Java: Calling variables of user input from one method to another

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

我的老师希望我们将ab和c变量输入到数组中,我知道它现在是一种将值放入数组中的相当手动的方法,但仍应为此目的工作。

这是我到目前为止的内容,感谢您的阅读。

编辑 :我又从头开始,我不知道如何正确地从我的方法返回信息,以便下一个可以使用它。 我一直在获取方法参数不适用的错误。 有任何想法吗?

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;
}

}

为了使您的当前代码正常工作,只需要进行少量更改:

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;
}

暂无
暂无

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

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