[AppleScript] 纯文本查看 复制代码 package Ten;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
/*
* 1.键盘输入10个数,放到数组中
a. 去除该数组中大于10的数
b. 将该数组中的数字写入到本地文件number.txt中
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
int[] arr = InPutNumber();
arr = delete(arr);
write(arr);
}
private static void write(int[] arr) throws IOException {
FileWriter fr = new FileWriter(new File("number.txt"));
fr.write(Arrays.toString(arr));
fr.close();
}
private static int[] delete(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 10) {
arr[i] = 10;
count++;
}
}
int[] newArray = new int[count];
for (int j = 0; j < (arr.length - count); j++) {
if (arr[j] != 0)
newArray[j] = arr[j];
}
return newArray;
}
private static int[] InPutNumber() {
int[] arr = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("请输入十个数字:");
for (int i = 0; i < 10; i++) {
arr[i] = sc.nextInt();
}
return arr;
}
}
|