gpt4 book ai didi

java - 程序不会打印出交集或差集!有什么建议么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:08 25 4
gpt4 key购买 nike

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:

最佳答案

我想我发现了你的错误:

  1. 您多次使用set.length(或setA.length或...)而不是方法参数(sizesizeA或...)。

    特别是在您的 sort 方法中,这是有问题的:排序后的数组将以 0 开头。您无法识别这一点,因为您忽略了 print 方法中的 0。按照打印交集和差值的方式打印它,您就会看到错误。

  2. 您将 intersect 作为参数传递给 difference 方法,而不是 difference

  3. 您在 intersection 方法中创建一个新数组。这仅替换本地数组,而不替换在 main 方法中创建的数组。 (int 数组用 0 初始化)

关于java - 程序不会打印出交集或差集!有什么建议么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27021429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com