java 用方法求解二次方程的Java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26768027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:33:53  来源:igfitidea点击:

Java program for solving quadratic equation using methods

java

提问by Joe Eastman

I have a java program written for solving the quadratic equation but the assignment requires me to have methods for each of the tasks: displaying the equation, determining if the equation has real solutions, calculating a solution, and displaying the solutions if they exist. I guess I haven't quite learned or came to a full understanding of implementing methods, here is the code I have thus far:

我有一个为求解二次方程而编写的 java 程序,但任务要求我为每个任务都有方法:显示方程,确定方程是否有实解,计算解,并显示解(如果存在)。我想我还没有完全了解或完全理解实现方法,这是我迄今为止的代码:

import java.util.Scanner;
public class QuadraticFormula {

public static void main(String[] args)
{
    //Creating scanner and variables
    Scanner s = new Scanner(System.in);
    System.out.println("Insert value for a: ");
    double a = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for b: ");
    double b = Double.parseDouble(s.nextLine());
    System.out.println("Insert value for c: ");
    double c = Double.parseDouble(s.nextLine());

    //Display format for negatives
    if (b > 0 && c > 0 ){
        System.out.println(a + "x^2 + " + b + "x + " + c + " =0");}
    if (b < 0 && c > 0 ){
        System.out.println(a + "x^2 " + b + "x + " + c + " =0");}
    if (b > 0 && c < 0 ){
        System.out.println(a + "x^2 + " + b + "x " + c + " =0");}
    if (b < 0 && c < 0 ){
        System.out.println(a + "x^2 " + b + "x " + c + " =0");}
    s.close();

    //The work/formula
    double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
    double answer2 = (-b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);

    //Display results and check if the solution is imaginary (real or not)
    if (Double.isNaN(answer1) || Double.isNaN(answer2))
    {
        System.out.println("Answer contains imaginary numbers");
    } else System.out.println("The values are: " + answer1 + ", " + answer2);
}
}

回答by Sri777

You are almost done with your assignment. All you need is a good understanding of the modularity.

你几乎完成了你的任务。您所需要的只是对模块化有一个很好的理解。

  1. displayEquation(double a, double b, double c);
  2. calculateSolution(double a, double b, double c);
  3. hasRealSolutions(double answer1, double answer2);
  4. displaySolutions(double answer1, double answer2);
  1. displayEquation(double a, double b, double c);
  2. 计算解决方案(双a,双b,双c);
  3. hasRealSolutions(double answer1, double answer2);
  4. 显示解决方案(双答案1,双答案2);

Of course this may look like some redundant or unnecessary for this small program but modularity concepts come in handy when you write really big programs. Please look at this http://www.javawithus.com/tutorial/call-by-value-and-call-by-reference

当然,对于这个小程序来说,这可能看起来有些多余或不必要,但是当您编写真正的大程序时,模块化概念会派上用场。请看这个http://www.javawithus.com/tutorial/call-by-value-and-call-by-reference

I think this is a assignment for you, believe me its a easy one. Go through the link you will find the answer yourself. Good luck with your programming.

我认为这是给你的任务,相信我,这很容易。通过链接,您将自己找到答案。祝你编程顺利。

回答by tww0003

I'll help you with making a method for this chunk of code (I'm not using all of it, because you'll learn more by applying it yourself)

我将帮助您为这块代码创建一个方法(我不会全部使用它,因为您将通过自己应用它来了解更多信息)

double answer1 = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);

To create a method, you need to first determine what it's return type is (if any), and what its parameters should be. Since you are working with doubles the return type for your "work the problem" method should be a double, and since you have three variables (a, b, c) those should be included in the methods parameters.

要创建一个方法,您首先需要确定它的返回类型是什么(如果有的话),以及它的参数应该是什么。由于您使用的是双精度型,因此您的“解决问题”方法的返回类型应该是双精度型,并且由于您有三个变量(a、b、c),它们应该包含在方法参数中。

When making parameters, you have to first say what type they are, and then give them a name. If you have more than 1 parameters, you have to separate them with a comma.

制作参数时,首先要说清楚它们是什么类型,然后再给它们起个名字。如果您有 1 个以上的参数,则必须用逗号分隔它们。

This is what the method should look it.

这才是应该看的方法吧。

private static double workTheFirstPart(double a, double b, double c)
{
  double ans = (-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
   return ans;
}

Just for clarity, this chunk of code will be placed outside of the main method (under the second to last curly bracket)

为了清楚起见,这块代码将放置在 main 方法之外(在倒数第二个大括号下)

In your main method, you should set the value of answer1 to

在您的主要方法中,您应该将 answer1 的值设置为

  double answer1 = workTheFirstPart(a, b, c);

Because the method returns a double, you can set answer1 equal to it.

因为该方法返回一个双精度值,您可以将 answer1 设置为等于它。

Hopefully this will get you going in the right direction and hopefully I didn't make any stupid mistakes (I've been at work all day, my brain is a little fried).

希望这能让你朝着正确的方向前进,希望我没有犯任何愚蠢的错误(我整天都在工作,我的大脑有点炸了)。

Good luck!

祝你好运!

回答by dogwin

Your code seems to be broken up well enough, using methods to handle the different parts is fairly straight foward. You'll need to make sure that you pass the necessary parameters to each method. For example, for determining if the solution is real, you'll need to pass your answer variables to check them. Furthermore, since you'll need to use this to display the solution, your method should return a boolean, to be checked in a separate method. Try to keep in mind what each method will need and what you may need to retrun from them. I hope this helps.

您的代码似乎分解得很好,使用方法来处理不同的部分是相当直接的。您需要确保将必要的参数传递给每个方法。例如,要确定解决方案是否真实,您需要传递答案变量以检查它们。此外,由于您需要使用它来显示解决方案,因此您的方法应该返回一个布尔值,以便在单独的方法中进行检查。尽量记住每种方法需要什么以及从它们中重新运行可能需要什么。我希望这有帮助。