Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 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.         float Sum = 0;
  6.         float Numerator;
  7.         System.out.println("Тема: Вычислить сумму");
  8.         System.out.println("Введите число n");
  9.         float n = input();
  10.         while (n < 1) {
  11.             System.out.println("Введите положительное число!");
  12.             n = input();
  13.         }
  14.         for (int i = 1; i <= n; i++) {
  15.             if (i % 2 == 0) {
  16.                 Numerator = 1;
  17.             } else {
  18.                 Numerator = -1;
  19.             }
  20.             Sum = Sum + (Numerator / (2 * i));
  21.         }
  22.         System.out.printf("Сумма равна: %.3f", Sum);
  23.     }
  24.  
  25.     public static float input() {
  26.         Scanner scan = new Scanner(System.in);
  27.         float value = 1;
  28.         try {
  29.             value = scan.nextFloat();
  30.             return value;
  31.         } catch (Throwable error) {
  32.             System.out.println("Ошибка! Введите число: ");
  33.             return input();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement