從這個程序中學到的:
          1 throws IOExceptiom 在這里不如用try catch方便
          2 已經創建類的方法的調用



          //計算一個一元二次方程
          import java.io.*;


          class InputData //建造一個從鍵盤獲取數據的累
          {
          ?private static String s="";
          ?public static void input()
          ?{
          ??BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          ??try
          ??{
          ???s=in.readLine();
          ??}catch (IOException e){}
          ??
          ?}
          ?static public float getFloat()
          ?{
          ??
          ??input();//調用input方法
          ??return Float.parseFloat(s);
          ? }
          }


          class Equation? //建造一個表示方程式的類
          {
          ?float a,b,c,disc=0;
          ?void input()? //三個參數輸入的方法
          ?{
          ??System.out.println("請輸入3個參數 a,b,c:");
          ??System.out.println("a=");
          ??a=InputData.getFloat();//調用自定義的InputData類中的input方法
          ??System.out.println("b=");
          ??b=InputData.getFloat();
          ??System.out.println("c=");
          ??c=InputData.getFloat();
          ?}
          ?void getRoots() //獲得根的方法
          ?{?? double x1, x2;
          ???? double realpart,imagpart;
          ??if (Math.abs(a)<1e-5)//判斷是否為一元二次方程
          ??{
          ???System.out.println("這不是一個一元二次方程");
          ????? System.exit(0);//退出程序
          ??}
          ??else
          ??{
          ???System.out.println("這是一個一元二次方程");
          ???disc=b*b-4*a*c;
          ?????
          ???if(disc<=1e-5)
          ???{
          ????x1=(-b)/(2*a);
          ????x2=(-b)/(2*a);
          ????System.out.println("此一元二次方程有兩個相等的根:x1=x2="+x1);
          ???}
          ???else if(disc>1e-5)
          ???{
          ????x1=(-b+Math.sqrt(disc))/(2*a);
          ????x2=(-b-Math.sqrt(disc))/(2*a);
          ????System.out.println("這個一元二次方程有兩個實根:"+"\nx1="+x1+"\nx2"+x2);
          ???}
          ???else
          ???{
          ????realpart=(-b)/2*a;
          ????imagpart=Math.sqrt(-disc);
          ????System.out.println("這個一元二次方程有兩個復數根:");
          ????System.out.println("x1="+realpart+"+"+imagpart+"i");
          ????System.out.println("x2="+realpart+"-"+imagpart+"i");
          ???}
          ??}
          ??
          ? }?
          }
          ?

          /**
          ?* @author Administrator
          ?*
          ?*/
          public class roots //主類
          {
          ?public static void main (String[] args)
          ?{
          ??Equation e=new Equation();
          ??e.input();
          ??e.getRoots();
          ??
          ?}
          }

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?

          ?


          ?


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 金坛市| 瓮安县| 尉犁县| 衡水市| 泽州县| 扶余县| 台中市| 延川县| 泌阳县| 巨鹿县| 宁武县| 云安县| 赞皇县| 贵阳市| 前郭尔| 广饶县| 名山县| 定边县| 秀山| 江源县| 民县| 防城港市| 江油市| 泾川县| 安国市| 崇文区| 奉化市| 云霄县| 东阳市| 浦城县| 三河市| 唐海县| 会理县| 咸丰县| 突泉县| 韶山市| 乐都县| 莆田市| 天柱县| 芮城县| 泗阳县|