Java: Viết chương trình Giải phương trình bậc 2.

Người đăng: share-nhungdieuhay on Thứ Hai, 24 tháng 6, 2013


import java.io.*;
public class Gptb2{
        public static void main(String args[]){
               InputStreamReader isr = new InputStreamReader(System.in);
               BufferedReader br = new BufferedReader(isr);
               try{
                       System.out.println("Nhap gia tri a");
                       String stra = br.readLine();
                       float a = Float.valueOf(stra).floatValue();
                      
                       System.out.println("Nhap gia tri b");
                       String strb = br.readLine();
                       float b = Float.valueOf(strb).floatValue();
                      
                       System.out.println("Nhap gia tri c");
                       String strc = br.readLine();
                       float c = Float.valueOf(strc).floatValue();

                       float delta = b*b - 4*a*c;
                       if(a==0)
                              if(b==0)
                                    if(c==0){
                                                System.out.print("PT co vo so nghiem");
                                    }
                                    else{
                                                System.out.print("PT vo nghiem");
                                    }
                              else{
                                    System.out.println("PT co nghiem duy nhat: " +c/b);
                              }
                       else{
                              if(delta<0){
                                    System.out.println("PT vo nghiem");
                              }
                              else if(delta>0){
                                    float x1 = (float)(-b + Math.sqrt(delta))/(2*a);
                                    float x2 = (float)(-b - Math.sqrt(delta))/(2*a);
                                    System.out.println("PT co 2 nghiem: x1= " +x1 + " va " +x2);
                              }
                              else{
                                    float x = (float)(-b)/(2*a);
                                    System.out.println("PT co nghiem kep: " +x);
                              }
                       }                      
               }
               catch(IOException ie){
                       System.out.println("Error: " +ie);
               }
        }
}

{ 0 nhận xét... read them below or add one }

Đăng nhận xét