很多“.class预期”和“不是声明”

时间:2013-04-21 22:54:47

标签: java

我正在尝试使程序接受两个3x3矩阵,然后添加或乘以它们。我收到了很多错误。

首先,当我尝试创建一个字符串以在这些行中显示整个矩阵时,出现了一堆“.class expected”错误:

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


当我对strB和strC进行另外两次迭代时,它会重复 当我查看这个错误时,我发现这些可能就是为什么会发生这种情况,扫描我的代码时似乎没有任何问题:

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.


我的下一个问题是当我尝试创建结果矩阵时。我在这段代码中得到了“not a statement”和“; expected”错误的混合:

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

<击>
错误交替出现;编译器首先在r1c []的左括号处生成“not a statement”,后面跟着“; expected”,在r1c [] =之间的空格处有一个箭头。第二次和第三次出现只是在代码中移动,重复位置(开括号,空格)。
感谢tacp正在解决这个问题!

这是我宣布所有变量的方式:

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


我真的不确定我做错了什么。这是我的所有代码.. code:p
这可能是非常基本的东西,但这是我用任何语言编写的第一个学期。所以我的故障排除技巧很少,我的实际编码技能也是如此。哈哈

所以,非常感谢您的所有帮助!

1 个答案:

答案 0 :(得分: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];
}