Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main{
- public static void main (String[] args) {
- Scanner scan = new Scanner(System.in);
- int factValue;
- int powerValue;
- float eps;
- boolean isNotCorrect;
- double currFunc, prevFunc;
- int numberOfIteration;
- int x;
- int multiplier;
- System.out.println("Данная программа бла бла бла");
- x = 0;
- do {
- isNotCorrect = false;
- System.out.println("Введите x");
- try {
- x = Integer.parseInt(scan.nextLine());
- } catch(NumberFormatException exception) {
- isNotCorrect = true;
- System.out.println("Ошибка ввода! Повторите попытку.");
- }
- } while(isNotCorrect); /* ввод x */
- eps = 0;
- do {
- isNotCorrect = false;
- System.out.println("Введите эпсилон");
- try {
- eps = Float.parseFloat(scan.nextLine());
- } catch(NumberFormatException exception) {
- isNotCorrect = true;
- }
- if ((isNotCorrect) || (eps > 1) || (eps < 0.00000001)) {
- isNotCorrect = true;
- System.out.println("Ошибка ввода! Повторите попытку.");
- }
- } while(isNotCorrect); /* ввод эпсилон */
- numberOfIteration = 0;
- currFunc = x;
- prevFunc = 0;
- multiplier = 1;
- System.out.print("sin(x) = ");
- System.out.println(String.format("%.10f", currFunc));
- while(Math.abs(currFunc - prevFunc) > eps ) {
- numberOfIteration++;
- prevFunc = currFunc;
- currFunc = 0;
- factValue = 1;
- for (int i = 1; i < 2 * numberOfIteration; i++) {
- factValue = factValue * i;
- }
- powerValue = x;
- for (int i = 1; i < (2 * numberOfIteration - 1); i++) {
- powerValue = powerValue * x;
- }
- if (numberOfIteration % 2 == 0) {
- multiplier = -1;
- }
- else {
- multiplier = 1;
- }
- currFunc = prevFunc + (multiplier * (powerValue / factValue));
- System.out.print("sin(x) = ");
- System.out.println(String.format("%.10f", currFunc));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement