二次公式代码中的错误(主要问题和1行问题)

时间:2014-01-25 04:37:43

标签: java eclipse

我看到了其他一些二次方程式问题,但似乎没有人遇到这个问题 本质上存在两个问题,一个是带有一个代码块,当通过插入“}”解决时会产生更多问题,另一个是不能运行的事实。我对java有点新,这是我第一个非常复杂的代码集,但我希望我只是犯了一些我现在看不到的小错误。

 package package10;

 public class QuadraticFormula 
 {

    public static void main(String[] args) 
    {

    }//Brought in the static void as Eclipse continually insisted this was neccesary. There is a main file I am supposed to be referencing
    //The file name is QuadraticFormulaMain.java and is in the same directory. Is there a way I should be writing this to reference it?

    public static String QuadraticFormula (int A, int B,  int C) 
    {

    }

    int A = 1;
    int B = -7;
    int C = 10;

    //Sets integers to easily testable numbers
    {
        System.out.println("Terry Peters");//Thats my Name
        Object findSolutions;
        System.out.println(findSolutions); 
    } 
    //Runs and Prints out the solution found below

    private static int discriminant (int A, int B, int C)  
    {
        //sets the discriminate with appropriate equation

        return (B * B)-(4*A*C);         
    }

    private static int numberOfSolutions (int A, int B, int C) 
    {
        //The actual numerOfSolutions, checks if the discriminant is positive negative or zero
        if (discriminant (A, B, C) > 0)
            return 2;
        if (discriminant (A, B, C) == 0 )
            return 1;
        if (discriminant (A, B, C) < 0)
            return 0;
     }

    //This performs the find Solutions String mentioned in the beginning
    //For whatever reason All return statements are showing errors, but says Eclipse has a quick fix.
    //This is a lie as all it says is "No Suggestions Avalible"
    private static String findSolutions (int A, int B, int C) 
    {
        if (numberOfSolutions (A, B, C) == 0);

        System.out.println ("There are no solutions");
        return "There are no solutions";

        if (numberOfSolutions (A, B, C) == 1)
            return ("There is one solution and it is" + plusSolution (A, B, C));

        if (numberOfSolutions (A, B, C) == 2)
            return "There are two solutions and they are " + plusSolution (A, B, C) + "and" + minusSolution (A, B, C);

    private static double plusSolution(int A, int B, int C) 
    {
        return ((B * -1) +  Math.sqrt((B * B)-(4*A*C))) / (2 * A);

    }

    private static double minusSolution (int A, int B, int C) 
    {
        return ((B * -1) - Math.sqrt((B * B)-(4*A*C))) / (2* A);

    }
}

3 个答案:

答案 0 :(得分:1)

这里有几个问题:

  • 您的构造函数QuadraticFormula()不应返回任何内容
  • 所有语句都需要与方法相关联(检查大括号并确保所有与方法关联的内容都在其中;如果方法中没有任何内容,则需要把它放在构造函数中,或为它创建一个方法。)
  • 普通类中的所有方法都需要以大括号结束,而不是以分号结尾。以分号结尾的方法可以在interfaces中找到,而不是普通类。
  • 如果您的代码应该从名为QuadraticFormulaMain.java的类执行,那么只有该类需要main()方法。
  • 如果要将字符串打印到终端屏幕,则应使用System.out.println(sampleString)。如果您要将该信息发送至QuadraticFormulaMain,则应return sampleString。你不应该同时做这两件事。
  • 您需要在QuadraticFormula的{​​{1}}方法中实例化QuadraticFormulaMain。您无需在main()中引用QuadraticFormulaMain

我没有为您编写正确的代码,因为它看起来像您需要练习。尝试修改代码只需查看这些建议,看看是否可以让它工作。与仅使用其他人的版本相比,您将学到很多东西。

答案 1 :(得分:1)

You Have made several misktakes here is running snippet 
 of your code.     

     public class QuadraticFormula 
    {
         public static void main(String[] args) 
         {
       int A = 1;
       int B = -7;
       int C = 10;
       System.out.println("Terry Peters");//Thats my Name

           System.out.println(findSolutions(A,B,C));
         }

        private static int discriminant (int A, int B, int C)  
        {
          //sets the discriminate with appropriate equation

            return (B * B)-(4*A*C);         
        }

       private static int numberOfSolutions (int A, int B, int C) 
       {
          //The actual numerOfSolutions, checks if the discriminant is positive    //negative    or zero
       if (discriminant (A, B, C) > 0)
           return 2;
       if (discriminant (A, B, C) == 0 )
           return 1;
       else
           return 0;

     }

    //This performs the find Solutions String mentioned in the beginning
    //For whatever reason All return statements are showing errors, but says Eclipse has  a  //quick fix.
   //This is a lie as all it says is "No Suggestions Avalible"
   private static String findSolutions (int A, int B, int C) 
   {
       if (numberOfSolutions (A, B, C) == 0)
       {
           System.out.println ("There are no solutions");
           return "There are no solutions";
       }   
       else if (numberOfSolutions (A, B, C) == 1)
       { 
           return ("There is one solution and it is" + plusSolution (A, B, C));
       }
       else
           return "There are two solutions and they are " + plusSolution (A, B, C) + "and"  + minusSolution (A, B, C);
   }   

   private static double plusSolution(int A, int B, int C) 
   {
       return ((B * -1) +  Math.sqrt((B * B)-(4*A*C))) / (2 * A);

   }

   private static double minusSolution (int A, int B, int C) 
   {
       return ((B * -1) - Math.sqrt((B * B)-(4*A*C))) / (2* A);

   }
 }

答案 2 :(得分:0)

试试这个

public class QuadraticFormula {

    int A = 1;
    int B = -7;
    int C = 10;

    public static void main(String[] args) {

        QuadraticFormula formula = new QuadraticFormula(2, 8, 4);
        System.out.println(formula.findSolutions());
    }//Brought in the static void as Eclipse continually insisted this was neccesary. There is a main file I am supposed to be referencing
//The file name is QuadraticFormulaMain.java and is in the same directory. Is there a way I should be writing this to reference it?

    public QuadraticFormula(int A, int B, int C) {
        this.A = A;
        this.B = B;
        this.C = C;
    }

//Sets integers to easily testable numbers
//Runs and Prints out the solution found below
    private static int discriminant(int A, int B, int C) {

        //sets the discriminate with appropriate equation
        return (B * B) - (4 * A * C);
    }

    private int numberOfSolutions(int A, int B, int C) {

        //The actual numerOfSolutions, checks if the discriminant is positive negative or zero
        if (discriminant(A, B, C) > 0) {
            return 2;
        } else if (discriminant(A, B, C) == 0) {
            return 1;
        } else {
            return 0;
        }
    }
//This performs the find Solutions String mentioned in the beginning
//For whatever reason All return statements are showing errors, but says Eclipse has a quick fix.
//This is a lie as all it says is "No Suggestions Avalible"

    private String findSolutions() {

        if (numberOfSolutions(A, B, C) == 1) {
            return ("There is one solution and it is" + plusSolution(A, B, C));
        } else if (numberOfSolutions(A, B, C) == 2) {
            return "There are two solutions and they are " + plusSolution(A, B, C) + "and" + minusSolution(A, B, C);
        } else {
            System.out.println("There are no solutions");
            return "There are no solutions";
        }
    }

    private static double plusSolution(int A, int B, int C) {
        return ((B * -1) + Math.sqrt((B * B) - (4 * A * C))) / (2 * A);

    }

    private static double minusSolution(int A, int B, int C) {
        return ((B * -1) - Math.sqrt((B * B) - (4 * A * C))) / (2 * A);

    }
}
  • 我所做的是成为有效的construtor public QuadraticFormula(...)
  • 将值传递给构造函数时。它将是类字段的值。
  • 对于findSolutions(),您不需要参数,只需使用类字段
  • 此外,我将部分if语句更改为else if,这样您就不会因为该方法需要返回值而收到错误。

输出

There are two solutions and they are -0.5857864376269049and-3.414213562373095