0

I'm trying to make a program to accept two 3x3 matrices, then add or multiply them. I'm getting lots of three different errors.

First, a bunch of ".class expected" errors when I try to create a string to display the whole matrix in these lines:

strA = "\n\n|" + r1a[];
strA += "|\n|" + r2a[];
strA += "|\n|" + r3a[];


It repeats when I do the other 2 iterations for strB and strC.
When I looked up this error, I found these as possibilities for why it's happening, none of which seem to be the issue when scanning my code:

Usually this is just a missing semicolon,
sometimes it can be caused by unbalanced () on the previous line.
sometimes it can be cause by junk on the previous line. This junk might be far to the right off the screen.
Sometimes it is caused by spelling the keyword if incorrectly nearby.
Sometimes it is a missing + concatenation operator.


My next issue is when I try to create the resulting matrix. I get a mixture of "not a statement" and "; expected" errors in this code:

r1c[] = r1a[] + r1b[];
r2c[] = r2a[] + r2b[];
r3c[] = r3a[] + r3b[];


The errors alternate; the compiler produces "not a statement" first with an arrow at the opening bracket of r1c[], followed by "; expected" with an arrow at the space between r1c[] =. The second and third occurrences simply move across the code, repeating the location (opening bracket, space).
Thanks to tacp for this one being resolved!

This is how I declared all of my variables:

import javax.swing.JOptionPane;

public class Matrices
{

    public static void main(String[] args)
    {


       int i = 0;
       double[] r1a = new double[3];        //row 1 of matrix a
       double[] r2a = new double[3];        //row 2 of matrix a
       double[] r3a = new double[3];        //row 3 of matrix a
       double[] r1b = new double[3];        //row 1 of matrix b
       double[] r2b = new double[3];        //row 2 of matrix b
       double[] r3b = new double[3];        //row 3 of matrix b
       double[] r1c = new double[3];        //row 1 of matrix c
       double[] r2c = new double[3];        //row 2 of matrix c
       double[] r3c = new double[3];        //row 3 of matrix c

       String strInput,         //holds JOption inputs
       strA,                    //holds matrix A
       strB,                    //holds matrix B
       strC;                    //holds matrix C


I really am not sure what I'm doing wrong. Here's all of my code.. code :p
This is probably something extremely basic, but this is my first semester of coding, ever, in any language.. So my troubleshooting skills are minimal, as are my actual coding skills. haha

So, all of your help is greatly appreciated!

4

1 回答 1

3
 strA = "\n\n|" + r1a[];
 strA += "|\n|" + r2a[];
 strA += "|\n|" + r3a[];

您需要指定类型strA和索引来访问数组元素r1a, r2ar3a

同样,您需要索引来访问数组(它们是表示矩阵行的数组):

r1c[] = r1a[] + r1b[];
r2c[] = r2a[] + r2b[];
r3c[] = r3a[] + r3b[];

例如:

for (int i = 0; i < 3; ++i)
{
   r1c[i] = r1a[i] + r1b[i];
}
于 2013-04-21T23:00:35.807 回答