By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Accessing variables to create matrices

P: 1
How do I create a program that takes user input and prints out a matrix. Ideally I would want to have the use to decide the number of matricies, the number of rows and columns and a sum, multiply, devide of the matricies when they are complete.

Thanks
-Ty

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.IOException;
  3.  
  4.  
  5. public class ScannerDemoforNetbeans
  6. {
  7.  
  8.     public static void main (String [] args)throws IOException
  9.     {
  10.    int x = 55;
  11.         Scanner s = new Scanner (System.in);
  12.     System.out.println ("What is your name?");
  13.     String name = s.nextLine ();
  14.      Scanner a = new Scanner (System.in);
  15.     System.out.println ("welcome, " + name + ", would you like to play a game with me?");
  16.     String answer = a.nextLine ();
  17.  
  18.     if (answer.equalsIgnoreCase("yes"))
  19.     {
  20.         System.out.println ("Will you Give me some numbers so I can make a matrix with them?");
  21.         Scanner nextanswer = new Scanner (System.in);
  22.         String q = nextanswer.nextLine();
  23. }
  24.     if (answer.equalsIgnoreCase("yes")) 
  25. {
  26.     System.out.println ("YAY!!! okay, How many matricies do you want");
  27.     Scanner i = new Scanner (System.in);
  28.     int number1;
  29.     number1 = i.nextInt(); 
  30.     System.out.println(number1);
  31.  
  32.    System.out.println ("Okay, you got it! How many rows  do you want");
  33.    Scanner rc= new Scanner (System.in);
  34.    public static rows;
  35.    rows= rc.nextInt();
  36.    System.out.println ("How many columns do you want?");
  37.    Scanner c = new Scanner (System.in);
  38.    public static columns;
  39.    columns = c.nextInt();
  40.    System.out.println ("Rows" + rows);
  41.    System.out.println ("Columns:" + columns);
  42.  
  43.  
  44. }
  45.    // }   
  46.    // public static void createarray (String [] args)
  47.    // {
  48.     int matWidth = ScannerDemoforNetbeans.columns;
  49.         int matHeight = ScannerDemoforNetbeans.rows;
  50.         int maxCellValue = 70;
  51.  
  52.                     int [][] mat1 = new int [matHeight][matWidth];
  53.                     int [][] mat2 = new int [matHeight][matWidth];
  54.                     int [][] sum = new int [matHeight][matWidth];
  55.  
  56.                     for (int i=0; i<matHeight; ++i)
  57.                     {
  58.                         for (int j=0; j<matWidth; ++j)
  59.                         {
  60.                             java.util.Random r = new java.util.Random();
  61.                             mat1 [i][j]= r.nextInt (maxCellValue);
  62.                             mat2 [i][j] = r.nextInt (maxCellValue);
  63.                         }
  64.                     }
  65.  
  66.                     print2Darray (mat1);
  67.                     System.out.println ();
  68.                     print2Darray (mat2);
  69.                     for (int i=0; i<matHeight; ++i)
  70.                     {
  71.                         for (int j=0; j<matWidth; ++j)
  72.                         {
  73.                             sum [i][j]= mat1 [i][j] + mat2 [i][j];
  74.                         }
  75.                     }
  76.                     System.out.println ();
  77.                     print2Darray (sum);
  78.     }
  79.     public static void print2Darray (int[][]arr)
  80.     {
  81.         for (int [] i : arr)
  82.  
  83.         {
  84.           for (int j : i)  
  85.           {
  86.               System.out.print (j + " ");
  87.           }
  88.         System.out.println();
  89.         }
  90.     System.out.println ();
  91.     }
  92. }
  93.  
Dec 20 '12 #1
Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,421
You haven't told us what's wrong with the code.
Dec 21 '12 #2

100+
P: 185
Your class does a big part of what your task. However, it has some pretty obvious compilation error.

I expect them to be typo errors but if you are new to java, please note that static variables can be defined only as class members. They can never be defined as local variables in a method.
Jan 5 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.