Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- float Sum = 0;
- float Numerator;
- System.out.println("Тема: Вычислить сумму");
- System.out.println("Введите число n");
- float n = input();
- while (n < 1) {
- System.out.println("Введите положительное число!");
- n = input();
- }
- for (int i = 1; i <= n; i++) {
- if (i % 2 == 0) {
- Numerator = 1;
- } else {
- Numerator = -1;
- }
- Sum = Sum + (Numerator / (2 * i));
- }
- System.out.printf("Сумма равна: %.3f", Sum);
- }
- public static float input() {
- Scanner scan = new Scanner(System.in);
- float value = 1;
- try {
- value = scan.nextFloat();
- return value;
- } catch (Throwable error) {
- System.out.println("Ошибка! Введите число: ");
- return input();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement