Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zadanie1;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class console {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int min = 0;
- int max = 0;
- int count = 0;
- int n = 4;
- double sum = 0;
- int a1 = 0;
- int an = 0;
- int wm = 0;
- int w = 0;
- double average = 0.0;
- System.out.println("Przedział z którego zostanie wylosowana liczba (MIN-MAX)");
- do {
- System.out.print("Podaj MIN:");
- try {
- min = Integer.parseInt(br.readLine());
- } catch (NumberFormatException nfe) {
- System.err.println("Musisz podać liczbe!");
- }
- } while (min == 0);
- System.out.print("Podaj MAX:");
- try {
- max = Integer.parseInt(br.readLine());
- } catch (NumberFormatException nfe) {
- System.err.println("Musisz podać liczbe!");
- }
- double randomDouble = Math.random();
- randomDouble = randomDouble * max + min;
- int randomInt = (int) randomDouble;
- System.out.println("Wylosowana liczba z przedziału <" + min + ", " + max + "> : " + randomInt);
- System.out.print("Podaj liczbe elementów ciągu:");
- try {
- count = Integer.parseInt(br.readLine());
- } catch (NumberFormatException nfe) {
- System.err.println("Musisz podać liczbe!");
- }
- System.out.println("Ciąg liczb podzielnych przez wylosowaną liczbę (" + randomInt + "): ");
- for (int i = 1; i < count + 1; i++) {
- System.out.print(randomInt * i + " ");
- if (i == n) {
- an = randomInt * i;
- }
- }
- System.out.println("");
- a1 = randomInt;
- System.out.println("Dla N = 4 wyrazów ciągu: A1 = " + randomInt + ", A" + n + " = " + an);
- sum = ((a1 + an) / 2) * n;
- System.out.println("Suma ciągu arytmetycznego wynosi: " + sum);
- System.out.println("Losowanie wag składowych <1, 4> dla otrzymanego ciągu: ");
- for (int i = 1; i < count + 1; i++) {
- double randomDoubleWeight = Math.random();
- randomDoubleWeight = randomDoubleWeight * 4 + 1;
- int randomWeight = (int) randomDoubleWeight;
- System.out.print(randomInt * i + " w[" + randomWeight + "], ");
- wm += (randomInt * i) * randomWeight;
- w += randomWeight;
- if (i == n) {
- an = randomInt * i;
- }
- }
- average = wm / w;
- System.out.println("");
- System.out.println("Średnia ważona wynosi: " + average);
- int c = 0;
- System.out.println("Wylosowano N = 4 liczby z przedziału <" + min + ", " + max * 10 + ">");
- for (int i = 0; i < 4; i++) {
- double randomDFour = Math.random();
- randomDFour = randomDFour * max * 10 + min;
- int randomFour = (int) randomDFour;
- System.out.print(randomFour + ", ");
- if (randomFour <= max) {
- c++;
- }
- }
- switch (c) {
- case 0:
- System.out.println("Żadna z wylosowanych liczb nie zawiera się w przedziale <" + min + ", " + max + ">");
- break;
- case 1:
- System.out.println(
- "Jedna z wylosowanych liczb zawiera się dodatkowo w przedziale <" + min + ", " + max + ">");
- break;
- case 2:
- System.out.println(
- "Dwie spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", " + max + ">");
- break;
- case 3:
- System.out.println(
- "Trzy spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", " + max + ">");
- break;
- case 4:
- System.out.println("Cztery spośród wylosowanych liczb zawierają się dodatkowo w przedziale <" + min + ", "
- + max + ">");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement