定义一个行数和列数相等的二维数组,并执行初始化,然后计算该数级两条对角线上的元素值之和。
public class Test {
public static void main(String[] args) {
int i, s = 0, w = 0;
int[][] a ={{2, 6, 2},{4, 9, 2},{2, 6, 1}} ;
int n = 3;
for (i = 0; i < n; i++) {
s = s + a[i][i];
w = w + a[i][n - 1 - i];
}
System.out.println(s);
System.out.print(w);
}
}
编写程序新建一文本文件,并对该文件进行复制操作
import java.util.*;
import java.io.*;
class FileControl{
public void create(String Save){
FileOutputStream output;
File out = new File(Save);
System.out.println("输入文件内容:");
Scanner in = new Scanner(System.in);
try {
output = new FileOutputStream(out);
byte input[] = in.nextLine().getBytes();
output.write(input);
output.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void copy(String file0,String file1) throws IOException{
FileInputStream read0 = new FileInputStream(file0);
byte[] ch = new byte[1024];
FileOutputStream write0 = new FileOutputStream(file1);
int m = 0;
while ((m = read0.read(ch)) != -1) {
write0.write(ch, 0, m);
}
read0.close();
write0.close();
}
}
public class Test {
public static void main(String[] args) throws IOException {
FileControl f=new FileControl();
f.create("src/step4/input/input.txt");
f.copy("src/step4/input/input.txt","src/step4/output/output.txt");
}
}