import java.util.Scanner;
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
int[] setA = new int[MAXSIZE];
int[] setB = new int[MAXSIZE];
int[] intersect = new int[MAXSIZE];
int[] difference = new int[MAXSIZE];
int sizeA, sizeB, interSize, diffSize;
System.out.print("How many numbers will be in the 1st set: ");
sizeA = kbd.nextInt();
while (sizeA > MAXSIZE) {
System.out
.print("Error: Set size is too large. Re-enter set size: ");
sizeA = kbd.nextInt();
}
System.out.println("Enter list of integers for 1st set: ");
getData(setA, sizeA);
sort(setA, sizeA);
System.out.println("The ascending order for 1st is:");
print(setA, sizeA);
System.out.print("How many numbers will be in the 2nd set: ");
sizeB = kbd.nextInt();
while (sizeB > MAXSIZE) {
System.out
.print("Error: Set size is too large. Re-enter set size: ");
sizeB = kbd.nextInt();
}
System.out.println("Enter list of integers for 2nd set: ");
getData(setB, sizeB);
sort(setB, sizeB);
System.out.println("The ascending order for the 2nd set is:");
print(setB, sizeB);
interSize = intersection(setA, setB, sizeA, sizeB, intersect);
System.out.print("The intersection of the two sets is: ");
for (int x = 0; x < interSize; x++) {
System.out.print(intersect[x] + " ");
}
diffSize = difference(setA, sizeA, setB, sizeB, intersect);
System.out.print("\n\nThe difference of A-B is: ");
for (int x = 0; x < diffSize; x++) {
System.out.print(difference[x] + " ");
}
}
public static void getData(int[] set, int size) {
for (int x = 0; x < size; x++) {
int num = kbd.nextInt();
int count = search(set, size, num);
if (count == 0)
set[x] = num;
else
x--;
}
}
public static int search(int[] set, int size, int num) {
int count = 0;
for (int x = 0; x < size; x++) {
if (num == set[x])
count++;
}
return count;
}
public static int difference(int[] setA, int sizeA, int[] setB, int sizeB,
int[] resultSet) {
int y = 0;
for (int x = 0; x < sizeA; x++) {
int num = setA[x];
int found = search(setB, sizeB, num);
if (found == 0) {
resultSet[y] = num;
y++;
}
}
return y;
}
public static void sort(int[] nums, int size) {
int temp;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = 0; j < nums.length - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
public static void print(int[] nums, int size) {
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
System.out.println(nums[i]);
}
}
}
public static int intersection(int[] setA, int[] setB, int size, int sizeB,
int[] resultSet) {
int count = 0;
for (int i = 0; i < setA.length; i++) {
for (int j = 0; j < setB.length; j++) {
if (setA[i] == setB[j]) {
count++;
break;
}
}
}
resultSet = new int[count];
count = 0;
for (int i = 0; i < setA.length; i++) {
for (int j = 0; j < setB.length; j++) {
if (setA[i] == setB[j]) {
resultSet[count++] = setA[i];
break;
}
}
}
return count;
}
}
要求是我必须使用方法和循环来达到解决方案。交集和差异方法也必须返回一个int作为赋值指令的一部分!
How many numbers will be in the 1st set: 3
Enter list of integers for 1st set:
34
2
56
The ascending order for 1st is:
2
34
56
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set:
56
2
33
6
The ascending order for the 2nd set is:
2
6
33
56
The intersection of the two sets is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The difference of A-B is:
答案 0 :(得分:1)
我想我发现了你的错误:
您多次使用set.length
(或setA.length
或...)代替方法参数(size
或sizeA
或...)。
特别是在您的sort
方法中,这是有问题的:排序的数组将以0
开头。您不会认识到这一点,因为您忽略了0
方法中的print
。以打印交叉点和差异的方式打印它,您将看到错误。
您将intersect
作为参数传递给difference
方法,而不是difference
。
您在intersection
方法中创建了一个新数组。这将仅替换本地数组,而不是main
方法中创建的数组。 (int
数组初始化为0
s)