2015-02-10 94 views
0

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

我的导师希望我们将b和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; 
} 

}

+0

当你在main中调用'getValues'时,它返回'double []'。您需要使用'double [] userInput = getValues();'来捕获返回值。然后将其作为输入参数传递给'calcDisc()'。您需要将'calcDisc'的方法签名更改为'calcDisc(double [] userInput)'。 – DavidS 2015-02-10 03:45:23

+0

如果您开始了解变量的“范围”,将来您会有更轻松的时间。谷歌“Java变量范围”了解更多。 – DavidS 2015-02-10 03:47:10

+0

谢谢戴维,这很有道理。我目前正在课堂上学习范围。我会在第二种方法开始时捕获返回值吗?或者在第一个结尾? – 2015-02-10 03:49:41

回答

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

这是巨大的最大值。我不知道我需要有我想要在我的主要分配返回值的变量。我需要让他们声明并等于我的方法,这样我才能将信息传递给下一个方法,我现在能够正确理解了吗?这是昨天发布的海报给我的,但没有点击我猜。 – 2015-02-11 06:23:14

+0

这是绝对正确的!在大多数情况下,当一个方法返回一个值时,它应该以某种方式进行分配和评估。即'''String test =“test”; test.substring(2);'不修改字符串,但返回修改后的值; 'String st = test.substring(2);'做的窍门 – 2015-02-11 06:35:30

相关问题