编写一个类,该类有一个SUM方法,该方法以数组作为参数,可以求数组所有元素的和。该类有一个JI方法,该方法以数组作为参数,可以求数组所有元素的乘积。编写另外一个类,在该类中创建一个数组,数组的值自己给定,然后创建上面编写的第一个类,进行测试SUM和JI方法,并将最后求得的和与乘积存入文件a.txt
[CODE]package array;
public class ArrayTest {
public int Sum(int[] array){
int sum=0;
for(int i:array){
sum+=i;
}
return sum;
}
public int Ji(int[] array){
int ji=1;
for(int i:array){
ji*=i;
}
return ji;
}
}[/CODE]
[CODE]package array;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayTest test=new ArrayTest();
int[] n={1,2,3,4,5,6,7,8,9};
System.out.println("数组的和是:"+test.Sum(n));
System.out.println("数组的积是:"+test.Ji(n));
}
}[/CODE]
package com.thinkbank;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public Test01(){
}
public int sum(int []array){
int s = 0;
for(int a:array){
s += a;
}
return s;
}
public int ji(int []array){
int j = 1;
for(int a:array){
j *= a;
}
return j;
}
}
class Test{
public Test(){
}
public static void main(String [] args){
int [] a = {1,2,3,4,5};
Test01 test01 = new Test01();
int s = test01.sum(a);
int j = test01.ji(a);
try {
FileWriter fileWriter = new FileWriter("C:\\a.txt",true);
fileWriter.write(new Integer(s).toString()+"\t" + new Integer(j).toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.thinkbank;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public Test01(){
}
public int sum(int []array){
int s = 0;
for(int a:array){
s += a;
}
return s;
}
public int ji(int []array){
int j = 1;
for(int a:array){
j *= a;
}
return j;
}
}
class Test{
public Test(){
}
public static void main(String [] args){
int [] a = {1,2,3,4,5};
Test01 test01 = new Test01();
int s = test01.sum(a);
int j = test01.ji(a);
try {
FileWriter fileWriter = new FileWriter("C:\\a.txt",true);
fileWriter.write(new Integer(s).toString()+"\t" + new Integer(j).toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这个程序出错啊,我用的是JDK1。6的,for(int a:array){这句好像不支持