簡體   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