Guest User

Untitled

a guest
Nov 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import static java.lang.Math.sqrt;
  2. import java.util.Scanner;
  3.  
  4. public class Bai2 {
  5.  
  6. private static Scanner scanner = new Scanner(System.in);
  7.  
  8. public static void main(String[] args) {
  9. System.out.print("Nhập hệ số bậc 2, a = ");
  10. float a = Bai2.scanner.nextFloat();
  11. System.out.print("Nhập hệ số bậc 1, b = ");
  12. float b = Bai2.scanner.nextFloat();
  13. System.out.print("Nhập hằng số tự do, c = ");
  14. float c = scanner.nextFloat();
  15. Bai2.giaiPTBac2(a, b, c);
  16. }
  17.  
  18. public static void giaiPTBac2(float a, float b, float c) {
  19. // kiểm tra các hệ số
  20. if (a == 0) {
  21. if (b == 0) {
  22. System.out.println("Phương trình vô nghiệm!");
  23. } else {
  24. System.out.println("Phương trình có một nghiệm: "
  25. + "x = " + (-c / b));
  26. }
  27. return;
  28. }
  29. // tính delta
  30. float delta = b * b - 4 * a * c;
  31. float x1;
  32. float x2;
  33. // tính nghiệm
  34. if (delta > 0) {
  35. x1 = (float) ((-b + Math.sqrt(delta)) / (2 * a));
  36. x2 = (float) ((-b - Math.sqrt(delta)) / (2 * a));
  37. System.out.println("Phương trình có 2 nghiệm là: "
  38. + "x1 = " + x1 + " và x2 = " + x2);
  39. } else if (delta == 0) {
  40. x1 = (-b / (2 * a));
  41. System.out.println("Phương trình có nghiệm kép: "
  42. + "x1 = x2 = " + x1);
  43. } else {
  44. System.out.println("Phương trình vô nghiệm!");
  45. }
  46. }
  47.  
  48. }
Add Comment
Please, Sign In to add comment