Advertisement
Egor_Vakar

lab3(java)

Sep 16th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         int i = 0;
  6.         double x =0, y, yi, eps = 0;
  7.         boolean isCorrect;
  8.         Scanner scan = new Scanner(System.in);
  9.         System.out.println("Введите eps:");
  10.         do
  11.         {
  12.             isCorrect = false;
  13.             try {
  14.                 eps = Double.parseDouble(scan.nextLine());
  15.             }
  16.             catch(Exception e) {
  17.                 System.out.println("eps введено некорректно!!!\nВведите eps:");
  18.                 isCorrect = true;
  19.             }
  20.             if (!isCorrect && (eps <= 0 || eps >= 1)){
  21.                 System.out.println("Заданное число не может являться eps!!!\nВведите eps(число от нуля до единицы):");
  22.                 isCorrect = true;
  23.             }
  24.         } while (isCorrect);
  25.         System.out.println("Введите x:");
  26.         do
  27.         {
  28.             isCorrect = false;
  29.             try {
  30.                 x = Double.parseDouble(scan.nextLine());
  31.             }
  32.             catch(Exception e) {
  33.                 System.out.println("x введён некорректно!!!\nВведите x:");
  34.                 isCorrect = true;
  35.             }
  36.         } while (isCorrect);
  37.         scan.close();
  38.         yi = 1;
  39.         do
  40.         {
  41.             i++;
  42.             y = yi;
  43.             if (y != 0)
  44.                 yi =(2 * y + x / y / y) / 3;
  45.         } while (Math.abs(y - yi) > eps);
  46.         if (y == 0)
  47.             System.out.println("Ошибка!!!\nВ ходе программы произошло деление на 0.\n");
  48.         else
  49.             System.out.println("Корень кубический из х = " + yi + "; Количество итераций : " + i + ".");
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement