• 大小: 8KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-06
  • 语言: Java
  • 标签: Java  

资源简介

一个小程序求解一次二次和三次方程解 一个小程序求解一次二次和三次方程解 一个小程序求解一次二次和三次方程解 一个小程序求解一次二次和三次方程解 一个小程序求解一次二次和三次方程解

资源截图

代码片段和文件信息



import java.io.IOException;
import java.util.Scanner;

public class SolveEquation{

void print()throws IOException{char ch=‘y‘;
        System.out.println(“求解几次方程?1:一次 2:二次 3:三次“);
        Scanner sLine=new Scanner(System.in);
        int pm=sLine.nextInt();
    if(pm==1){
      System.out.println(“你选择的是一元一次方程:“);
      SolveEquation.SolveSimpleEquation fc=new SolveEquation().new SolveSimpleEquation();
      fc.SolveSimpleEquation();
    }
     else if(pm==2){
       System.out.println(“你选择的是一元二次方程:“);
       SolveEquation.SolveQuadraticEquation fc=new SolveEquation().new SolveQuadraticEquation();
       fc.SolveQuadraticEquation();
    }
     else if(pm==3){
       System.out.println(“你选择的是一元三次方程:“);
       SolveEquation.SolveCubicEquation fc=new SolveEquation().new SolveCubicEquation();
       fc.SolveCubicEquation();
     }
      else {print();}
      System.out.println(“你是否想继续:(y/n)“);
      ch=(char)System.in.read();
      System.in.skip(2);
         if(ch==‘y‘)print();   
            else if(ch==‘n‘){System.out.println(“Good luck!“);}
                 else print();
}
    public static void main (String args[]) throws IOException{
         SolveEquation se=new SolveEquation();
          se.print();
   }


interface I1{void SolveCubicEquation();}
interface I2{void SolveQuadraticEquation();}
interface I3{void SolveSimpleEquation();}


class SolveCubicEquation implements I1{
public void SolveCubicEquation(){
    System.out.println(“请输入形如一元三次方程mx^3+nx^2+tx+s=0的四个系数“);
    Scanner sce=new Scanner(System.in);
    double m=sce.nextDouble();
    double n=sce.nextDouble();
    double t=sce.nextDouble();
    double s=sce.nextDouble();
    if(m==0)System.out.print(“输入错误!“);
       else {
           double a=n/m;
           double b=t/m;
           double c=s/m;
           double q=(a*a-3*b)/9;
           double r=(2*a*a*a-9*a*b+27*c)/54;
                 if(r*r                      System.out.println(“此方程有三个解:“);
                      t=Math.acos(r/Math.sqrt(q*q*q));
                      double x1=-2*Math.sqrt(q)*Math.cos(t/3)-a/3;
                      double x2=-2*Math.sqrt(q)*Math.cos((t+2*Math.PI)/3)-a/3;
                      double x3=-2*Math.sqrt(q)*Math.cos((t-2*Math.PI)/3)-a/3;
                      System.out.println(“x1=“+x1+““+“x2=“+x2+““+“x3=“+x3);
                  }
                         else{
                             System.out.println(“此方程只有一个解:“);
                             int sgn=(r>=0)?1:-1;
                             double u=-sgn*Math.pow((Math.abs(r)+Math.sqrt(r*r-q*q*q))1./3);
                             double v=(u!=0)?q/u:0;
                             double x1=u+v-a/3;
                             System.out.println(“x=“+x1);
                         }
        }   
       }
   
}


class SolveQuadraticEquation implements I2{
public void SolveQuadraticEquation(){   
    System.out.println(“请输入形如一元二次方程ax^2+bx+c=0的三个

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         307  2011-05-09 15:29  equation\.classpath
     文件         384  2011-05-09 15:29  equation\.project
     文件         634  2011-05-09 15:29  equation\.settings\org.eclipse.jdt.core.prefs
     文件         200  2011-05-09 17:06  equation\bin\SolveEquation$I1.class
     文件         204  2011-05-09 17:06  equation\bin\SolveEquation$I2.class
     文件         201  2011-05-09 17:06  equation\bin\SolveEquation$I3.class
     文件        2206  2011-05-09 17:06  equation\bin\SolveEquation$SolveCubicEquation.class
     文件        1759  2011-05-09 17:06  equation\bin\SolveEquation$SolveQuadraticEquation.class
     文件        1236  2011-05-09 17:06  equation\bin\SolveEquation$SolveSimpleEquation.class
     文件        2102  2011-05-09 17:06  equation\bin\SolveEquation.class
     文件        4492  2011-05-09 17:06  equation\src\SolveEquation.java

评论

共有 条评论