Advertisement
Vladislav8653

laba 1_3 java

Sep 27th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. import java.util.Scanner;
  2. import static java.lang.Math.abs;
  3. public class epsilon {
  4.     public static void main(String[] args) {
  5.         int n = 0;
  6.         double y0 = 1, x = 0, y, eps = 0;
  7.         boolean isIncorrect;
  8.         Scanner scanner = new Scanner(System.in);
  9.         do {
  10.             isIncorrect = false;
  11.             System.out.println("Пожалуйста, введите число EPS (EPS > 0): ");
  12.             try {
  13.                 eps = Double.parseDouble(scanner.nextLine());
  14.             } catch (Exception e) {
  15.                 System.out.println("Пожалуйста, введите число. ");
  16.                 isIncorrect = true;
  17.             }
  18.             if (!isIncorrect && (eps <= 0)) {
  19.                 System.out.println("Число Эпсилон должно быть положительным.");
  20.                 isIncorrect = true;
  21.             }
  22.         } while (isIncorrect);
  23.         System.out.println("Введите x: ");
  24.         do {
  25.             isIncorrect = false;
  26.             try {
  27.                 x = Double.parseDouble(scanner.nextLine());
  28.             } catch (Exception e) {
  29.                 System.out.println("Пожалуйста, введите число. ");
  30.                 isIncorrect = true;
  31.             }
  32.         } while (isIncorrect);
  33.         do {
  34.             y = y0;
  35.             y0 = 0.5 * (y + (x / y));
  36.             n = n + 1;
  37.         } while (abs(y - y0) >= eps);
  38.         System.out.println("Корень из x = " + y0);
  39.         System.out.println("Число итераций: " + n);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement