Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Problem2 {
- public static void main(String args[]) throws java.io.UnsupportedEncodingException{
- PrintStream ps = new PrintStream(System.out, true, "UTF-8");
- Scanner in = new Scanner(System.in);
- int n, sum;
- n = 0;
- sum = 0;
- boolean isNotCorrect;
- isNotCorrect = true;
- do {
- try {
- ps.println("Введите n (количество суммируемых элементов): ");
- n = Integer.parseInt(in.nextLine());
- if (n < 1)
- ps.println("Введите натуральное число!");
- else isNotCorrect = false;
- }
- catch (NumberFormatException err) {
- ps.println("Некорректный ввод данных!");
- }
- } while (isNotCorrect);
- n++;
- for (int i = 1; i < n; i++)
- if (i % 2 == 1)
- sum = sum - i * i;
- else
- sum = sum + i * i;
- ps.print("Значение искомой суммы: ");
- ps.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement