Untitled
raw download clone
TEXT
views 22
,
size 2812 b
package com.company;
import java.util.Scanner;


public class Main {



    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Wprowadz n:");
        int n = keyboard.nextInt();
        Macierz p = new Macierz();
        p.wypełnijMacierz(n);
                p.wypisz(n);
        System.out.println("");
                p.wypelnij2(5 , 5);
                p.wypisz2(5);
        System.out.println("");
        p.sumaprzekatnej(n);
        p.maxwiersz(n);
    }
}


---------------------------------------------

package com.company;
import java.util.Random;

public class Macierz {
    Random r = new Random();
    private int n;

    int tab[][] = new int[10][10];

    public int nwd(int a, int b) {
        if (a == b) return b;
        else if (a == b) return a;
        else if (b == 0) return b;
        else {
            while (a != b) {
                if (a > b)
                    a -= b;
                else
                    b -= a;
            }
            return a;
        }
    }

    public void wypełnijMacierz(int n) {

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int a = i;
                int b = j;
                tab[i][j] = nwd(a,b);
            }
        }
    }

    public void wypisz(int n) {
        for (int i = 0; i < n; i++) {
            System.out.println();
            for (int j = 0; j < n; j++) {
                System.out.print(" " + tab[i][j] + " ");
            }
        }
    }

    public void wypelnij2(int n , int a) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tab[i][j] = r.nextInt(a) + 1;
            }
        }

    }

    public void wypisz2(int n) {
        for (int i = 0; i < n; i++) {
            System.out.println();
            for (int j = 0; j < n; j++) {
                System.out.print(" " + tab[i][j] + " ");
            }
        }

    }

    public void sumaprzekatnej(int n) {
        int suma = 0;
        for (int i = 0; i < n; i++) {
            suma = suma + tab[i][i];
        }
        System.out.println("Suma przekatnej wynosi : " + suma);
    }

    public void maxwiersz(int n) {
        int sumaR;
        int przechowaj = 0;
        for (int i = 0; i < n; i++) {
            sumaR = 0;
            for (int j = 0; j < n; j++) {
                sumaR = sumaR + tab[i][j];
               if(sumaR > przechowaj)
                   przechowaj = sumaR;
            }
            System.out.println("Suma " + (i + 1) + " wiersza: " + sumaR);


        }
        System.out.println("Maksymalna suma wiersza to " + " " + przechowaj);
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.