import java.util.*;
import java.math.*;public class Test {

float x1,x2,y1,y2,x;
public static void main(String []args)
{
float x1,x2,y1,y2,x;
do{
System.out.println("please input two numbers:");
Scanner in = new Scanner(System.in);
x1 = in.nextFloat();
x2 = in.nextFloat();
y1 = f(x1);
y2 = f(x2); }while(y1*y2>=0);
x =root(x1,x2);
System.out.printf("A root of equation is %8.4f",x);
}
public static float f(float x){
float y;
y = ((x-5)*x+16)*x-80;
return y;
}

public static float root(float x1,float x2){
float y1,y,x;
y1=f(x1);
do
{
  x=xpoint(x1,x2);
  y=f(x);
  if(y1*y>=0)
  {
   y1=y;
   x1=x;
  }
  else
     x2=x;
  }while((Math.abs(y))>=0.0001);
return x;
}

public static float xpoint(float x1,float x2){
float x;
x = (f(x2)*x1-f(x1)*x2)/(f(x2)-f(x1));
return x;
}
}
/*
please input two numbers:
2
6     //输入一个数后回车再输入另一个数*/
A root of equation is   5.0000

解决方案 »

  1.   

    要是x前系数未知怎么搞??
    f(x)=a*x*x*x+b*x*x+c*x+d
    怎么输入abcd啊
      

  2.   

    哦哦.我求的是y = ((x-5)*x+16)*x-80; 
      

  3.   

    public static float f(float x){
    float y;
    float a,b,c,d;
    Scanner in = new Scanner(System.in);
    System.out.print("input a:");
    a = in.nextFloat();
    System.out.print("input b:");
    b = in.nextFloat();
    System.out.print("input c:");
    c = in.nextFloat();
    System.out.print("input d:");
    d = in.nextFloat();
    y=a*x*x*x+b*x*x+c*x+d;
    return y;
    }
    应该这样写,当输入的x,y,a,b,c,d不满足时,重复输入.
      

  4.   

    呵呵...这次应该行了!!!
    import java.util.*;
    import java.math.*;public class Test {

    float x1,x2,y1,y2,x;
    static float a,b,c,d;
    public static void main(String []args)
    {
    float x1,x2,y1,y2,x;
    do{
    System.out.println("please input two numbers:");
    Scanner in = new Scanner(System.in);
    x1 = in.nextFloat();
    x2 = in.nextFloat();

    System.out.print("input a:");
    a = in.nextFloat();
    System.out.print("input b:");
    b = in.nextFloat();
    System.out.print("input c:");
    c = in.nextFloat();
    System.out.print("input d:");
    d = in.nextFloat();
    y1 = f(x1);
    y2 = f(x2); }while(y1*y2>=0);
    x =root(x1,x2);
    System.out.printf("A root of equation is %8.4f",x);
    }
    public static float f(float x){
    float y;

    y=a*x*x*x+b*x*x+c*x+d;
    return y;
    }

    public static float root(float x1,float x2){
    float y1,y,x;
    y1=f(x1);
    do
    {
      x=xpoint(x1,x2);
      y=f(x);
      if(y1*y>=0)
      {
       y1=y;
       x1=x;
      }
      else
         x2=x;
      }while((Math.abs(y))>=0.0001);
    return x;
    }

    public static float xpoint(float x1,float x2){
    float x;
    x = (f(x2)*x1-f(x1)*x2)/(f(x2)-f(x1));
    return x;
    }
    }
      

  5.   

    我验证了,是对的.可能是你输入不符合要求,或者是你还没有理解弦截法求方程根吧!
    例如这样输入:
    please input two numbers:
    1
    2
    input a:-2
    input b:1
    input c:1
    input d:1
    A root of equation is   1.2337
      

  6.   

    我现在大二,java初学者,很多地方还不太懂,大家以后互相学习学习!!!呵呵...
      

  7.   

    --------------------Configuration: <Default>--------------------
    please input two numbers:
    1
    2
    input a:-2
    input b:1
    input c:1
    input d:1
    please input two numbers:
    我的结果啊,用的六楼的代码