Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

MGT

By: a guest on Dec 14th, 2010  |  syntax: Java 5  |  size: 1.35 KB  |  views: 73  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Random;
  7.  
  8. public class Main {
  9.  
  10.     static Random r = new Random();
  11.  
  12.     public static int readInt() {
  13.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  14.         int n = 0;
  15.         try {
  16.             n = Integer.parseInt(br.readLine());
  17.         } catch (IOException ex) {
  18.             readInt();
  19.         } catch (NumberFormatException ex) {
  20.             System.out.println("Please, enter a digit");
  21.             readInt();
  22.         }
  23.         return n;
  24.     }
  25.  
  26.     public static int[][] generateGraph(int n) {
  27.         int graph[][] = new int[n][n];
  28.         for (int i=0;i<n;i++)
  29.             for (int j=i;j<n;j++)
  30.                 if (i==j)
  31.                     graph[i][j] = 0;
  32.                 else
  33.                     graph[i][j] = graph[j][i] = r.nextInt(9);
  34.         return graph;
  35.     }
  36.  
  37.     public static void printGraph(int[][] graph, int n) {
  38.         for (int i=0;i<n;i++) {
  39.             for (int j=0;j<n;j++)
  40.                 System.out.print(graph[i][j]+" ");
  41.             System.out.println();
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         System.out.println("Enter amount of vertexes in graph: ");
  47.         int n = readInt();
  48.         int[][] graph = generateGraph(n);
  49.         printGraph(graph, n);
  50.     }
  51.  
  52. }