在java中用参数实现方法

在java中用参数实现方法,java,Java,我是一名新的java程序员,我正试图编写一个程序,通过在这个类中实现roots()方法来查找二次方程的根 我想我已经知道了如何实现这个等式,但是返回语句说:错误类型不匹配:无法从double转换为Set 如何修复此错误 谢谢大家! package warmup; import java.util.Set; public class Quadratic { /** * Find the integer roots of a quadratic equation, ax^2 + bx +

我是一名新的java程序员,我正试图编写一个程序,通过在这个类中实现roots()方法来查找二次方程的根

我想我已经知道了如何实现这个等式,但是返回语句说:错误类型不匹配:无法从double转换为Set

如何修复此错误

谢谢大家!

 package warmup;

import java.util.Set;

 public class Quadratic {

/**
 * Find the integer roots of a quadratic equation, ax^2 + bx + c = 0.
 * @param a coefficient of x^2
 * @param b coefficient of x
 * @param c constant term.  Requires that a, b, and c are not ALL zero.
 * @return all integers x such that ax^2 + bx + c = 0.
 */
public static Set<Integer> roots(int a, int b, int c) {

    //my code so far
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
    return q;
}


/**
 * Main function of program.
 * @param args command-line arguments
 */
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Integer> result = roots(1, -4, 3);
    System.out.println(result);
  }


}
包装预热;
导入java.util.Set;
公共类二次型{
/**
*求二次方程的整数根,ax^2+bx+c=0。
*@param a系数x^2
*@param b x的系数
*@param c常量项。要求a、b和c不都为零。
*@返回所有整数x,使ax^2+bx+c=0。
*/
公共静态集合根(int a、int b、int c){
//到目前为止我的代码
双q=-b+(数学sqrt(数学pow(b,2)-4*a*c)/2*a);
返回q;
}
/**
*程序的主要功能。
*@param args命令行参数
*/
公共静态void main(字符串[]args){
System.out.println(“对于方程x^2-4x+3=0,可能的解为:”);
设置结果=根(1,-4,3);
系统输出打印项次(结果);
}
}

为什么要使用
Set
作为返回类型,而可以使用
Double
。 将
Set
替换为
Double
,您的问题就解决了


注意:-您正试图返回double,但您的方法返回类型已设置….

为什么要使用
Set
作为返回类型,而可以使用
double
public class Quadratic  {

public static double roots(int a, int b, int c) {

    //my code so far
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
    return q;
}
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    double result = roots(1, -4, 3);
    System.out.println(result);
  }
Set
替换为
Double
,您的问题就解决了

注意:-您正试图返回double,但您的方法返回类型已设置…

public class Quadratic  {

public static double roots(int a, int b, int c) {

    //my code so far
    double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
    return q;
}
public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    double result = roots(1, -4, 3);
    System.out.println(result);
  }
}


}

在根函数中返回的是双变量,而不是Set对象。因此,类型不匹配。将其修改为:

public static double roots(int a, int b, int c) {

double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
return q;
}
同时确保获得该函数的双精度结果。 更改此项:

 Set<Integer> result = roots(1, -4, 3);

这应该是可行的。

您在根函数中返回的是一个双变量,而不是Set对象。因此,类型不匹配。将其修改为:

public static double roots(int a, int b, int c) {

double q = -b + (Math.sqrt(Math.pow(b, 2)-4*a*c)/2*a);  
return q;
}
同时确保获得该函数的双精度结果。 更改此项:

 Set<Integer> result = roots(1, -4, 3);

这应该是可行的。

以下代码包含对问题中代码的一些修复,请参阅代码中的注释以获得进一步解释:

public static Set<Double> roots(int a, int b, int c) {
    Double x = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
    Set<Double> result = new HashSet<>(); // return a set that contains the results
    result.add((-b + x)/ 2 * a); // -b should be divided by 2a as well
    result.add((-b - x)/ 2 * a); // -b should be divided by 2a as well
    return result;
}

public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Double> result = roots(1, -4, 3); // the returned type is Set<Double> not Set<Integer> 
    System.out.println(result);
}

以下代码包含对问题中代码的一些修复,请参阅代码中的注释以获取进一步解释:

public static Set<Double> roots(int a, int b, int c) {
    Double x = Math.sqrt(Math.pow(b, 2) - 4 * a * c);
    Set<Double> result = new HashSet<>(); // return a set that contains the results
    result.add((-b + x)/ 2 * a); // -b should be divided by 2a as well
    result.add((-b - x)/ 2 * a); // -b should be divided by 2a as well
    return result;
}

public static void main(String[] args) {
    System.out.println("For the equation x^2 - 4x + 3 = 0, the possible solutions are:");
    Set<Double> result = roots(1, -4, 3); // the returned type is Set<Double> not Set<Integer> 
    System.out.println(result);
}

方法的返回类型是
Set
。返回
集合
意味着您可以返回零个、一个或多个元素。这是适当的,因为在一般情况下,二次方程有多个根

表示返回的集合元素必须是整数,这很奇怪,因为二次方程的根通常是实数,有时是复数。我猜你真正想要的是
Set

如果确实要返回单个元素,则需要返回一个已添加了相关根的集合。调用
Collections.singleton()
是一种方便的方法

值得一读:


方法的返回类型是
Set
。返回
集合
意味着您可以返回零个、一个或多个元素。这是适当的,因为在一般情况下,二次方程有多个根

表示返回的集合元素必须是整数,这很奇怪,因为二次方程的根通常是实数,有时是复数。我猜你真正想要的是
Set

如果确实要返回单个元素,则需要返回一个已添加了相关根的集合。调用
Collections.singleton()
是一种方便的方法

值得一读:

导入java.util.Set;
导入java.util.HashSet;
公共类二次型{
/**
*求二次方程的整数根,ax^2+bx+c=0。
*@param a系数x^2
*@param b x的系数
*@param c常量项。要求a、b和c不都为零。
*@返回所有整数x,使ax^2+bx+c=0。
*/
公共静态集合根(int a、int b、int c){
Set result=new HashSet();
//到目前为止我的代码
双p=(-b-(数学sqrt(数学功率(b,2)-4*a*c))/2*a;
结果:添加(p);
双q=(-b+(数学sqrt(数学pow(b,2)-4*a*c))/2*a;
结果:添加(q);
返回结果;
}
/**
*程序的主要功能。
*@param args命令行参数
*/
公共静态void main(字符串[]args){
System.out.println(“对于方程x^2-4x+3=0,可能的解为:”);
设置结果=根(1,-4,3);
系统输出打印项次(结果);
}
}
导入java.util.Set;
导入java.util.HashSet;
公共类二次型{
/**
*求二次方程的整数根,ax^2+bx+c=0。
*@param a系数x^2
*@param b x的系数
*@param c常量项。要求a、b和c不都为零。
*@返回所有整数x,使ax^2+bx+c=0。
*/
公共静态集合根(int a、int b、int c){
Set result=new HashSet();
//到目前为止我的代码
双p=(-b-(数学sqrt(数学功率(b,2)-4*a*c))/2*a;
结果:添加(p);
双q=(-b+(数学sqrt(数学pow(b,2)-4*a*c))/2*a;
结果:添加(q);
返回结果;
}
/**
*程序的主要功能。
*@param args命令行参数
*/
公共静态void main(字符串[]args){
System.out.println(“对于方程x^2-4x+3=0,可能的解为:”);
设置结果=根(1,-4,3);
系统输出打印项次(结果);
}
}

错误是不言自明的:您声明该方法在方法签名中返回
Set
,但返回