AnimePsix

Untitled

Sep 19th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main2 {
  4.     public static void main(String[] args) {
  5.         System.out.println("Введите Число суммирований ");
  6.         Scanner in = new Scanner(System.in);
  7.         final String errorMessage = "ОШИБКА, введите целое положительное число (от 0 до " + Integer.MAX_VALUE + ")";
  8.         boolean isCorrect = true;
  9.         int n = 0;
  10.         int i;
  11.         double sum = 1;
  12.         do {
  13.             try {
  14.                 n = in.nextInt();
  15.                 if (n>0)
  16.                     isCorrect = false;
  17.                 else
  18.                     System.out.println(errorMessage);
  19.             }
  20.             catch (Exception e) {
  21.                 System.out.println(errorMessage);
  22.                 in.nextLine();
  23.             }
  24.         }
  25.         while (isCorrect);
  26.         for (i = 2; i < (n + 1); i++)
  27.             sum += 1.0 / (i * i * i);
  28.         System.out.println("Сумма равна " + sum);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment