简体   繁体   English

程序不会打印出交叉点或差异! 有什么建议么?

[英]Program won't print out the intersection or difference! Any suggestions?

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;
    }
}

The requirements are that I must use methods and loops to reach the solution. 要求是我必须使用方法和循环才能找到解决方案。 Also the intersection and difference methods MUST return an int as part of the assignment instructions! 同样,相交和差分方法必须返回int作为赋值指令的一部分!

Test Input: 测试输入:

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: 

I think i found your errors: 我想我发现了您的错误:

  1. You use set.length (or setA.length or ...) instead of the method parameter ( size or sizeA or ...) multiple times. set.length使用set.length (或setA.length或...)而不是方法参数( sizesizeA或...)。

    Especially in your sort method this is problematic: the sorted arrays will start with 0 s. 尤其是在您的sort方法中,这是有问题的:排序后的数组将从0 s开始。 You don't recognize this, since you ignore 0 s in your print method. 您不认识到这一点,因为在print方法中忽略了0 s。 Print it the way you print the intersection and difference and you'll see the error. 以打印相交和差异的方式进行打印,您会看到错误。

  2. You pass intersect as parameter to your difference method instead of difference . 您将intersect作为参数传递给difference方法,而不是difference

  3. You create a new array in your intersection method. 您在intersection方法中创建一个新数组。 This replaces only the local array, not the one created in your main method. 这仅替换本地数组,而不替换在main方法中创建的数组。 ( int arrays are initialized with 0 s) int数组初始化为0 s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM