2013-04-21 47 views
0

我试图让程序接受两个3x3矩阵,然后添加或乘以它们。我得到了很多三个不同的错误。很多“.class期望”和“不是一个声明”

首先,“预期的.class”错误,当我试图创建一个字符串,以显示这些行整个矩阵一堆:

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


它,当我做其他2次重复迭代对于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. 


我的下一个问题是,当我尝试以创建最终的矩阵。我得到“没有声明”的混合物和“预期”在此代码中的错误:

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


的错误备用;编译器首先在r1c []的左括号处用箭头产生“不是语句”,然后在r1c [] =之间的空格处带有“; expected”。第二次和第三次出现只是移动代码,重复该位置(左括号,空格)。
感谢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
这可能是非常基本的东西,但这是我编程的第一个学期,以任何语言编写。所以我的故障排除技巧是最小的,我的实际编码技能也是如此。 haha

因此,非常感谢您的帮助!

+0

什么'R1A [] + r1b []'应该是?并添加'r1a []'到一个字符串? – 2013-04-21 22:58:48

+0

你想追加到方法体外的类变量字符串吗?这会导致你看到的一些编译器错误。在你的问题中添加你班级的***签名,以及所有变量的声明。为了简洁起见,您可以省略这些方法。 – Perception 2013-04-21 22:58:50

+0

这就是你如何声明多维数组例如:'double [] [] anArray = new double [5] [5]'。这将创建5行和5列。并用他们的行列组合引用它们。 – 2013-04-21 23:04:52

回答

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]; 
} 
+0

对不起,您指定类型和索引是什么意思? 另外,是否没有简单的方法将整个数组添加到另一个数组?我必须通过for循环添加单个双打吗? – Riqqu 2013-04-21 23:58:55

+0

@Riqqu类型的变量表示可以对它们应用哪种操作。例如,int a表示a是int,并且可以对其执行+, - ,*,/或%操作。所以strA应该是字符串类型。在Java中,要将两个数组添加到另一个数组,您应该使用for循环或每个循环。 – taocp 2013-04-22 00:02:27

+0

我已经声明类型。 String strInput, strA, strB, strC; – Riqqu 2013-04-22 00:32:00