Advertisement
dxvmxnd

Untitled

Oct 8th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main{
  4. public static void main (String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. int factValue;
  8. int powerValue;
  9. float eps;
  10. boolean isNotCorrect;
  11. double currFunc, prevFunc;
  12. int numberOfIteration;
  13. int x;
  14. int multiplier;
  15.  
  16. System.out.println("Данная программа бла бла бла");
  17.  
  18. x = 0;
  19. do {
  20. isNotCorrect = false;
  21. System.out.println("Введите x");
  22. try {
  23. x = Integer.parseInt(scan.nextLine());
  24. } catch(NumberFormatException exception) {
  25. isNotCorrect = true;
  26. System.out.println("Ошибка ввода! Повторите попытку.");
  27. }
  28. } while(isNotCorrect); /* ввод x */
  29.  
  30. eps = 0;
  31. do {
  32. isNotCorrect = false;
  33. System.out.println("Введите эпсилон");
  34. try {
  35. eps = Float.parseFloat(scan.nextLine());
  36. } catch(NumberFormatException exception) {
  37. isNotCorrect = true;
  38. }
  39. if ((isNotCorrect) || (eps > 1) || (eps < 0.00000001)) {
  40. isNotCorrect = true;
  41. System.out.println("Ошибка ввода! Повторите попытку.");
  42. }
  43. } while(isNotCorrect); /* ввод эпсилон */
  44.  
  45. numberOfIteration = 0;
  46. currFunc = x;
  47. prevFunc = 0;
  48. multiplier = 1;
  49.  
  50. System.out.print("sin(x) = ");
  51. System.out.println(String.format("%.10f", currFunc));
  52.  
  53. while(Math.abs(currFunc - prevFunc) > eps ) {
  54. numberOfIteration++;
  55. prevFunc = currFunc;
  56. currFunc = 0;
  57.  
  58. factValue = 1;
  59. for (int i = 1; i < 2 * numberOfIteration; i++) {
  60. factValue = factValue * i;
  61. }
  62.  
  63. powerValue = x;
  64. for (int i = 1; i < (2 * numberOfIteration - 1); i++) {
  65. powerValue = powerValue * x;
  66. }
  67.  
  68. if (numberOfIteration % 2 == 0) {
  69. multiplier = -1;
  70. }
  71. else {
  72. multiplier = 1;
  73. }
  74.  
  75. currFunc = prevFunc + (multiplier * (powerValue / factValue));
  76.  
  77. System.out.print("sin(x) = ");
  78. System.out.println(String.format("%.10f", currFunc));
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement