Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Random;
- public class Main {
- static Random r = new Random();
- public static int readInt() {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n = 0;
- try {
- n = Integer.parseInt(br.readLine());
- } catch (IOException ex) {
- readInt();
- } catch (NumberFormatException ex) {
- System.out.println("Please, enter a digit");
- readInt();
- }
- return n;
- }
- public static int[][] generateGraph(int n) {
- int graph[][] = new int[n][n];
- for (int i=0;i<n;i++)
- for (int j=i;j<n;j++)
- if (i==j)
- graph[i][j] = 0;
- else
- graph[i][j] = graph[j][i] = r.nextInt(9);
- return graph;
- }
- public static void printGraph(int[][] graph, int n) {
- for (int i=0;i<n;i++) {
- for (int j=0;j<n;j++)
- System.out.print(graph[i][j]+" ");
- System.out.println();
- }
- }
- public static void main(String[] args) {
- System.out.println("Enter amount of vertexes in graph: ");
- int n = readInt();
- int[][] graph = generateGraph(n);
- printGraph(graph, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement