Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Smth {
- private int[] arr;
- private int n=5;
- private int sum=0, avg, min, rand;
- public static void main (String[] args) {
- Smth ayylmao = new Smth();
- ayylmao.showArr();
- System.out.println(ayylmao.arrSum());
- System.out.println(ayylmao.arrAvg());
- System.out.println(ayylmao.arrMin());
- ayylmao.arrSwitch();
- }
- //конструктор
- Smth(){
- n=getsize();
- arr=new int[n];
- for (int i=0; i<arr.length; i++)
- {
- arr[i]= (int) (Math.random() * 10);
- }
- }
- public void arrSwitch () {
- System.out.print("\nЭлемент массива # ");
- if (arrChet() == 1) {
- System.out.print(rand);
- System.out.print(" со значением ");
- System.out.print(arr[rand]);
- System.out.print(" - чётный");
- }
- else {
- System.out.print(rand);
- System.out.print(" со значением ");
- System.out.print(arr[rand]);
- System.out.print(" - нечётный");
- }
- }
- public int arrChet ()
- {
- rand=(int) (Math.random() * n);
- int temp=0;
- switch (arr[rand]) {
- case 0: temp= 1;
- case 1: temp= 2;
- case 2: temp= 1;
- case 3: temp= 2;
- case 4: temp= 1;
- case 5: temp= 2;
- case 6: temp= 1;
- case 7: temp= 2;
- case 8: temp= 1;
- case 9: temp= 2;
- }
- return temp;
- }
- public int getsize ()
- {
- System.out.println("\nEnter array size: ");
- Scanner myObj = new Scanner(System.in);
- String m = myObj.nextLine();
- return Integer.parseInt(m);
- }
- public void showArr()
- {
- System.out.println("\nYour array: ");
- int i=0;
- while (i<arr.length)
- {
- System.out.println(arr[i]);
- i++;
- }
- }
- public int arrSum()
- {
- System.out.println("\nSum result: ");
- int i=0;
- do {
- sum+=arr[i];
- i++;
- } while (i<arr.length);
- return sum;
- }
- public int arrAvg()
- {
- int counter=0;
- System.out.println("\nAverage: ");
- for (int i=0; i<arr.length; i++){
- avg+=arr[i];
- counter++;
- }
- return avg/counter;
- }
- public int arrMin()
- {
- System.out.println("\nMin: ");
- min=arr[0];
- for (int i:arr)
- {
- if (i<n) { if (min>arr[i]) min=arr[i];}
- }
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement