Accessing variables to create matrices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • squibber321
    New Member
    • Dec 2012
    • 1

    #1

    Accessing variables to create matrices

    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

    Code:
    import java.util.Scanner;
    import java.io.IOException;
    
    
    public class ScannerDemoforNetbeans
    {
     
        public static void main (String [] args)throws IOException
        {
       int x = 55;
            Scanner s = new Scanner (System.in);
        System.out.println ("What is your name?");
        String name = s.nextLine ();
         Scanner a = new Scanner (System.in);
        System.out.println ("welcome, " + name + ", would you like to play a game with me?");
        String answer = a.nextLine ();
        
        if (answer.equalsIgnoreCase("yes"))
        {
            System.out.println ("Will you Give me some numbers so I can make a matrix with them?");
            Scanner nextanswer = new Scanner (System.in);
            String q = nextanswer.nextLine();
    }
        if (answer.equalsIgnoreCase("yes")) 
    {
        System.out.println ("YAY!!! okay, How many matricies do you want");
        Scanner i = new Scanner (System.in);
        int number1;
        number1 = i.nextInt(); 
        System.out.println(number1);
        
       System.out.println ("Okay, you got it! How many rows  do you want");
       Scanner rc= new Scanner (System.in);
       public static rows;
       rows= rc.nextInt();
       System.out.println ("How many columns do you want?");
       Scanner c = new Scanner (System.in);
       public static columns;
       columns = c.nextInt();
       System.out.println ("Rows" + rows);
       System.out.println ("Columns:" + columns);
       
      
    }
       // }   
       // public static void createarray (String [] args)
       // {
        int matWidth = ScannerDemoforNetbeans.columns;
            int matHeight = ScannerDemoforNetbeans.rows;
            int maxCellValue = 70;
                  
                        int [][] mat1 = new int [matHeight][matWidth];
                        int [][] mat2 = new int [matHeight][matWidth];
                        int [][] sum = new int [matHeight][matWidth];
                        
                        for (int i=0; i<matHeight; ++i)
                        {
                            for (int j=0; j<matWidth; ++j)
                            {
                                java.util.Random r = new java.util.Random();
                                mat1 [i][j]= r.nextInt (maxCellValue);
                                mat2 [i][j] = r.nextInt (maxCellValue);
                            }
                        }
                
                        print2Darray (mat1);
                        System.out.println ();
                        print2Darray (mat2);
                        for (int i=0; i<matHeight; ++i)
                        {
                            for (int j=0; j<matWidth; ++j)
                            {
                                sum [i][j]= mat1 [i][j] + mat2 [i][j];
                            }
                        }
                        System.out.println ();
                        print2Darray (sum);
        }
        public static void print2Darray (int[][]arr)
        {
            for (int [] i : arr)
        
            {
              for (int j : i)  
              {
                  System.out.print (j + " ");
              }
            System.out.println();
            }
        System.out.println ();
        }
    }
    Last edited by Niheel; Dec 21 '12, 04:31 AM. Reason: CODe and question should be in one post
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You haven't told us what's wrong with the code.

    Comment

    • Anas Mosaad
      New Member
      • Jan 2013
      • 185

      #3
      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.

      Comment

      Working...