Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package zadanie1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class console {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12. int min = 0;
  13. int max = 0;
  14. int count = 0;
  15. int n = 4;
  16.  
  17. double sum = 0;
  18. int a1 = 0;
  19. int an = 0;
  20.  
  21. int wm = 0;
  22. int w = 0;
  23. double average = 0.0;
  24.  
  25. System.out.println("Przedział z którego zostanie wylosowana liczba (MIN-MAX)");
  26. do {
  27. System.out.print("Podaj MIN:");
  28. try {
  29. min = Integer.parseInt(br.readLine());
  30. } catch (NumberFormatException nfe) {
  31. System.err.println("Musisz podać liczbe!");
  32. }
  33. } while (min == 0);
  34.  
  35. System.out.print("Podaj MAX:");
  36. try {
  37. max = Integer.parseInt(br.readLine());
  38. } catch (NumberFormatException nfe) {
  39. System.err.println("Musisz podać liczbe!");
  40. }
  41.  
  42. double randomDouble = Math.random();
  43. randomDouble = randomDouble * max + min;
  44. int randomInt = (int) randomDouble;
  45.  
  46. System.out.println("Wylosowana liczba z przedziału <" + min + ", " + max + "> : " + randomInt);
  47. System.out.print("Podaj liczbe elementów ciągu:");
  48. try {
  49. count = Integer.parseInt(br.readLine());
  50. } catch (NumberFormatException nfe) {
  51. System.err.println("Musisz podać liczbe!");
  52.  
  53. }
  54.  
  55. System.out.println("Ciąg liczb podzielnych przez wylosowaną liczbę (" + randomInt + "): ");
  56. for (int i = 1; i < count + 1; i++) {
  57. System.out.print(randomInt * i + " ");
  58. if (i == n) {
  59. an = randomInt * i;
  60. }
  61. }
  62. System.out.println("");
  63.  
  64. a1 = randomInt;
  65. System.out.println("Dla N = 4 wyrazów ciągu: A1 = " + randomInt + ", A" + n + " = " + an);
  66.  
  67. sum = ((a1 + an) / 2) * n;
  68.  
  69. System.out.println("Suma ciągu arytmetycznego wynosi: " + sum);
  70.  
  71. System.out.println("Losowanie wag składowych <1, 4> dla otrzymanego ciągu: ");
  72. for (int i = 1; i < count + 1; i++) {
  73. double randomDoubleWeight = Math.random();
  74. randomDoubleWeight = randomDoubleWeight * 4 + 1;
  75. int randomWeight = (int) randomDoubleWeight;
  76. System.out.print(randomInt * i + " w[" + randomWeight + "], ");
  77. wm += (randomInt * i) * randomWeight;
  78. w += randomWeight;
  79. if (i == n) {
  80. an = randomInt * i;
  81. }
  82. }
  83. average = wm / w;
  84. System.out.println("");
  85. System.out.println("Średnia ważona wynosi: " + average);
  86.  
  87. int c = 0;
  88. System.out.println("Wylosowano N = 4 liczby z przedziału <" + min + ", " + max * 10 + ">");
  89. for (int i = 0; i < 4; i++) {
  90. double randomDFour = Math.random();
  91. randomDFour = randomDFour * max * 10 + min;
  92. int randomFour = (int) randomDFour;
  93. System.out.print(randomFour + ", ");
  94. if (randomFour <= max) {
  95. c++;
  96. }
  97. }
  98. switch (c) {
  99. case 0:
  100. System.out.println("Żadna z wylosowanych liczb nie zawiera się w przedziale <" + min + ", " + max + ">");
  101. break;
  102. case 1:
  103. System.out.println(
  104. "Jedna z wylosowanych liczb zawiera się dodatkowo w przedziale <" + min + ", " + max + ">");
  105. break;
  106.  
  107. case 2:
  108. System.out.println(
  109. "Dwie spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", " + max + ">");
  110. break;
  111.  
  112. case 3:
  113. System.out.println(
  114. "Trzy spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", " + max + ">");
  115. break;
  116.  
  117. case 4:
  118. System.out.println("Cztery spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", "
  119. + max + ">");
  120. break;
  121.  
  122. }
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement