本人电子信息工程专业,但只学过一点点C语言,这几天的学习进度能赶上,以下是同学自己提高需求的作业题,我也拿来快乐,初学编程,代码难免有些啰嗦,有不好的地方也欢迎指正。再说一句,敲代码使我快乐!敲代码使我快乐!敲代码使我快乐!
1、生成两个长度为10的数组,元素是1到100的整数
2、判断两个数组中不同元素的个数,并输出
3、输出不同元素
import java.util.Random;
public class Happy1 {
public static void main(String[] args) {
Random r = new Random();
int[] arr1 = new int[10];
int[] arr2 = new int[10];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = r.nextInt(100) + 1;
System.out.print(arr1[i] + " ");
}
System.out.println();
for (int m = 0; m < arr1.length; m++) {
arr2[m] = r.nextInt(100) + 1;
System.out.print(arr2[m] + " ");
}
swap(arr1, arr2);
}
public static void swap(int[] arr1, int[] arr2) {
int a = 0;
int b = 0;
int c = 0;
StringBuilder str = new StringBuilder();
String s = "";
str.append(s);
for (int i = 0; i < arr1.length; i++) {
for (int k = 0; k < i + 1; k++) {
if (arr1[i] != arr1[k] || i == k) {
for (int j = 0; j < arr2.length; j++) {
if (arr1[i] == arr2[j]) {
a++;
}
}
b = arr1[i];
}else{
a++;
}
}
if (a == 0) {
str.append(b).append(" ");
c++;
}
else{
a = 0;
}
}
for (int i = 0; i < arr2.length; i++) {
for (int k = 0; k < i + 1; k++) {
if (arr2[i] != arr2[k] || i == k) {
for (int j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
a++;
}
}
b = arr2[i];
}else{
a++;
}
}
if (a == 0) {
str.append(b).append(" ");
c++;
}
else{
a = 0;
}
}
if (c == 0) {
System.out.println();
System.out.println("两个数组的元素均相同");
} else {
System.out.println();
System.out.println(str.toString());
System.out.println("不同的个数有:" + c);
}
}
}
|
|