简体   繁体   English

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

[英]Java program for solving quadratic equation using methods

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. 我有一个用于解决二次方程式的Java程序,但是作业要求我拥有用于​​每个任务的方法:显示方程式,确定方程式是否具有实际解,计算解以及显示解(如果存在)。 I guess I haven't quite learned or came to a full understanding of implementing methods, here is the code I have thus far: 我想我还没有完全了解或完全了解实现方法,这是到目前为止的代码:

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

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); displayEquation(double a,double b,double c);
  2. calculateSolution(double a, double b, double c); computeSolution(double a,double b,double c);
  3. hasRealSolutions(double answer1, double answer2); hasRealSolutions(double answer1,double answer2);
  4. displaySolutions(double answer1, double answer2); displaySolutions(double answer1,double answer2);

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. 祝您编程愉快。

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. 我希望这有帮助。

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. 由于您使用的是double,因此“工作问题”方法的返回类型应该为double,并且由于您拥有三个变量(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. 如果您有多个参数,则必须用逗号分隔。

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. 因为该方法返回一个double,所以可以将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! 祝好运!

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

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