调用主方法

戴维森

我已经完成了一些我认为可以回忆主要方法的代码。如果用户输入 Y 或 y,我希望程序重新启动。该算法已经运行良好,我只需要循环部分的解决方案。我试图在命令提示符下运行它

import java.util.Scanner;

class Quadratic{

    public static void main(String args[]){
        Scanner input = new Scanner (System.in);

        System.out.print("\nax^2 + bx + c = 0");

        //input value of a.
        System.out.print("\n\na = ");
        int a = input.nextInt();

        //input value of b.
        System.out.print("b = ");
        int b = input.nextInt();

        //input value of c.
        System.out.print("c = ");
        int c = input.nextInt();

        System.out.print("\n");

        //output a-part of the equation.
        if(a != 0){
            if(a == -1) {System.out.print("- ");}
            else if(a == 1) {System.out.print("");}
            else if(a < 0)  {System.out.print("- " + Math.abs(a));}
            else if(a > 0)  {System.out.print(a);}
            System.out.print("x^2 ");}
        //output b-part of the equation.
        if(b != 0){
            if(b == -1) {System.out.print("- ");}
            else if(b == 1) {System.out.print("+ ");}
            else if(b < 0)  {System.out.print("- " + Math.abs(b));}
            else if(b > 0)  {System.out.print("+ " + b);}
            System.out.print("x ");}
        //output c-part of the equation.
        if(c != 0){
            if(c < 0)   {System.out.print("- " + Math.abs(c));}
            else if(c > 0)  {System.out.print("+ " + c);}}
        //if everything is not zero.
        if(a != 0 && b != 0 && c != 0)  {System.out.print(" = 0\n\n");}
        //if everything is zero.
        else if(a == 0 && b == 0 && c == 0) {System.out.print("0 + 0 + 0 = 0\n\n");}

        Quadratic quad = new Quadratic();
        quad.findAnswer(a, b, c);
        quad.additionalRoot(a, b, c);

        quad.runAgain();
    }

    //find the final answer.
    private void findAnswer(int a, int b, int c){
        if(findDiscriminant(a, b, c) < 0){
            System.out.print("Imaginary Root");
        }

        else if(findDiscriminant(a, b, c) == 0){
            System.out.print("One Twin Root");
            findTwoRoot(a, b, c);
        }

        else if(findDiscriminant(a, b, c) > 0){
            System.out.print("Two Different Root");
            findTwoRoot(a, b, c);
        }
    }

    //find the discriminant of the quadratic-equation.
    private int findDiscriminant(int a, int b, int c){
        int discriminant = b*b - 4*a*c;
        return discriminant;
    }

    //find the two root of the quadratic-equation.
    private void findTwoRoot(int a, int b, int c){
        double x1 = (-b + Math.sqrt(findDiscriminant(a, b, c)))/2*a;
        double x2 = (-b - Math.sqrt(findDiscriminant(a, b, c)))/2*a;

        if(x1 == x2)    {System.out.print("\n\nx = " + x1);}
        else    {System.out.print("\nx1 = " + x1 + "\nx2 = " + x2);}
    }

    //find the additional value by root.
    private void additionalRoot(int a, int b, int c){
        double addition = -b/a;
        System.out.print("\n\nX1 + X2 = " + addition);
        double multiplication = c/a;
        System.out.print("\nX1 * X2 = " + multiplication);
        double xp = -b/2*a;
        double yp = findDiscriminant(a, b, c) / -4*a;
        System.out.print("\n\nCoordinate of the extreme value = (" + xp + ", " + yp + ")");
    }

    //rerun the main method.
    private void runAgain(){
        Scanner input = new Scanner(System.in);

        System.out.print("\n\nAgain? (Y/N)\n");
        String repeat = input.next();
        while(repeat == "Y" || repeat == "y"){
            runProgram();
            System.out.print("\n\nAgain? (Y/N)\n");
            repeat = input.next();
        }
    }

    //the main program.
    private void runProgram(){
        Scanner input = new Scanner (System.in);

        System.out.print("\nax^2 + bx + c = 0");

        //input value of a.
        System.out.print("\n\na = ");
        int a = input.nextInt();

        //input value of b.
        System.out.print("b = ");
        int b = input.nextInt();

        //input value of c.
        System.out.print("c = ");
        int c = input.nextInt();

        System.out.print("\n");

        //output a-part of the equation.
        if(a != 0){
            if(a == -1) {System.out.print("- ");}
            else if(a == 1) {System.out.print("");}
            else if(a < 0)  {System.out.print("- " + Math.abs(a));}
            else if(a > 0)  {System.out.print(a);}
            System.out.print("x^2 ");}
        //output b-part of the equation.
        if(b != 0){
            if(b == -1) {System.out.print("- ");}
            else if(b == 1) {System.out.print("+ ");}
            else if(b < 0)  {System.out.print("- " + Math.abs(b));}
            else if(b > 0)  {System.out.print("+ " + b);}
            System.out.print("x ");}
        //output c-part of the equation.
        if(c != 0){
            if(c < 0)   {System.out.print("- " + Math.abs(c));}
            else if(c > 0)  {System.out.print("+ " + c);}}
        //if everything is not zero.
        if(a != 0 && b != 0 && c != 0)  {System.out.print(" = 0\n\n");}
        //if everything is zero.
        else if(a == 0 && b == 0 && c == 0) {System.out.print("0 + 0 + 0 = 0\n\n");}

        Quadratic quad = new Quadratic();
        quad.findAnswer(a, b, c);
        quad.additionalRoot(a, b, c);

        quad.runAgain();
    }
}

我应该怎么做才能回忆主要方法?

杰里米·卡汉

我做了一些改变。首先,我将 runAgain 设为一个布尔函数,并将主要部分放在一个 while 循环中,以检查玩家是否想再次玩游戏。此外,我尝试不重新打开扫描仪,因为它在 jdoodle 上给了我一个错误(它还希望该类是公开的)。最后,我重写了字符串比较以使用 .equal。reuslting 程序似乎可以执行您想要的操作。

import java.util.Scanner;

public class Quadratic {

public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    boolean keepPlaying = true;
    while (keepPlaying) {
        System.out.print("\nax^2 + bx + c = 0");

        //input value of a.
        System.out.print("\n\na = ");
        int a = input.nextInt();

        //input value of b.
        System.out.print("b = ");
        int b = input.nextInt();

        //input value of c.
        System.out.print("c = ");
        int c = input.nextInt();

        System.out.print("\n");

        //output a-part of the equation.
        if (a != 0) {
            if (a == -1) {
                System.out.print("- ");
            } else if (a == 1) {
                System.out.print("");
            } else if (a < 0) {
                System.out.print("- " + Math.abs(a));
            } else if (a > 0) {
                System.out.print(a);
            }
            System.out.print("x^2 ");
        }
        //output b-part of the equation.
        if (b != 0) {
            if (b == -1) {
                System.out.print("- ");
            } else if (b == 1) {
                System.out.print("+ ");
            } else if (b < 0) {
                System.out.print("- " + Math.abs(b));
            } else if (b > 0) {
                System.out.print("+ " + b);
            }
            System.out.print("x ");
        }
        //output c-part of the equation.
        if (c != 0) {
            if (c < 0) {
                System.out.print("- " + Math.abs(c));
            } else if (c > 0) {
                System.out.print("+ " + c);
            }
        }
        //if everything is not zero.
        if (a != 0 && b != 0 && c != 0) {
            System.out.print(" = 0\n\n");
        }
        //if everything is zero.
        else if (a == 0 && b == 0 && c == 0) {
            System.out.print("0 + 0 + 0 = 0\n\n");
        }

        Quadratic quad = new Quadratic();
        quad.findAnswer(a, b, c);
        quad.additionalRoot(a, b, c);

        keepPlaying = quad.runAgain(input);
    }
}

//find the final answer.
private void findAnswer(int a, int b, int c) {
    if (findDiscriminant(a, b, c) < 0) {
        System.out.print("Imaginary Root");
    } else if (findDiscriminant(a, b, c) == 0) {
        System.out.print("One Twin Root");
        findTwoRoot(a, b, c);
    } else if (findDiscriminant(a, b, c) > 0) {
        System.out.print("Two Different Root");
        findTwoRoot(a, b, c);
    }
}

//find the discriminant of the quadratic-equation.
private int findDiscriminant(int a, int b, int c) {
    int discriminant = b * b - 4 * a * c;
    return discriminant;
}

//find the two root of the quadratic-equation.
private void findTwoRoot(int a, int b, int c) {
    double x1 = (-b + Math.sqrt(findDiscriminant(a, b, c))) / 2 * a;
    double x2 = (-b - Math.sqrt(findDiscriminant(a, b, c))) / 2 * a;

    if (x1 == x2) {
        System.out.print("\n\nx = " + x1);
    } else {
        System.out.print("\nx1 = " + x1 + "\nx2 = " + x2);
    }
}

//find the additional value by root.
private void additionalRoot(int a, int b, int c) {
    double addition = -b / a;
    System.out.print("\n\nX1 + X2 = " + addition);
    double multiplication = c / a;
    System.out.print("\nX1 * X2 = " + multiplication);
    double xp = -b / 2 * a;
    double yp = findDiscriminant(a, b, c) / -4 * a;
    System.out.print("\n\nCoordinate of the extreme value = (" + xp + ", " + yp + ")");
}

//see whether to rerun the main method.
private boolean runAgain(Scanner input) {

    System.out.print("\n\nAgain? (Y/N)\n");
    String repeat = input.next();
    return (repeat.equals("Y") || repeat.equals("y"));
}




}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章