到最后的逻辑错误

时间:2014-11-26 04:30:02

标签: java arrays

我完成了本周到期的项目。它是一个程序,按升序获取数组的交集和差异。一切都在方法的范围内发挥作用,但我试图在用户的反应之间加入一个线条,但我必须在某个地方删除某些东西并且不能使用。找到我的日志到那个部分。它假设只是重复do while循环中的内容,但是当我第二次再次进行时,看起来它似乎是将我设置的数字乘以int。我知道它必须是一个简单的逻辑错误,但我的头脑在这一点上被炸了。

这是我到目前为止所得到的:

import java.util.Scanner;
public class settest {

    public static Scanner kbd = new Scanner(System.in);

    /*
     * This method will retrieve "size" number of unique
     * integers from keyboard and placed into an array set. 
     */
    public static void getData(int [] set, int size){

        boolean isUnique=false;
        int input; int count=0;

        while(count < size){
            input = kbd.nextInt();
            for(int i=0; i < size; i++) 
            {
                if (input == set[i]) 
                {
                    isUnique = true;
                }   
            }

            if (!isUnique) 
            {
                set[count] = input;
                count++;
            }
            isUnique = false;
        }

    }

    /*
     * This method will calculate the intersection of two sets of arrays.
     * It needs to receive two sets of arrays, their size, and a holding
     * array as parameters from the main program.
     * When finished the method will return the numbers that are in common
     * in both sets (resultSize) 
     */
    public static int intersection(int [] setA, int sizeA , int [] setB, 
            int sizeB, int [] resultSet){

        int resultSize=0;
        for(int x=0; x<sizeA; x++)
        {
            for(int y=0; y<sizeB; y++)
            {
                if(setA[x]==setB[y])
                {
                    resultSet[resultSize]=setA[x];
                    resultSize++;
                }
            }
        }

        return resultSize;
    }

    /*
     * Calculates the difference (A-B) of the two array sets. 
     * It will received the set [], size of correct array and in 
     * ascending order as parameters.
     * Returns the correct array size for the received resultSet[]
     */
    public static int difference(int [] setA, int sizeA , int [] setB, 
            int sizeB, int [] resultSet){

        int resultSize=0; boolean sameVal=false;

        for(int x=0; x<sizeA; x++ ){
            for(int y=0; y<sizeB; y++){
                if(setA[x]==setB[y]){
                    sameVal=true;
                }
            }
            if(!sameVal){
                resultSet[resultSize]=setA[x];
                resultSize++;
            }
            sameVal=false;
        }
        return resultSize;
    }

    /*
     * Method will sort the numbers in set[] array into ascending order. 
     */
    public static void sort(int [] set, int size){

        for(int x=0; x< size-1; x++)
        {
            for(int y=x+1; y<size; y++)
            {   
                if(set[x] > set[y])
                {
                    int temp = set[x];
                    set[x] = set[y];
                    set[y] = temp;
                }
            }     
        }
    }

    public static final int MAXSIZE = 20;

    public static void main(String[] args) {
        int [] setA = new int[MAXSIZE];
        int [] setB = new int[MAXSIZE];
        int [] resultSet = new int[MAXSIZE];
        int sizeA=0, sizeB=0;int resultSize=0;
        String repeat;

        do{
            System.out.println("ENTER 2 SETS OF NUMBERS.\n");
            System.out.println("HOW MANY NUMBERS IN YOUR FIRST SET?");
            sizeA=kbd.nextInt();
            for(int i=0; i<sizeA; i++){

                if (sizeA>MAXSIZE){
                    System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED."
                            + "ENTER A DIFFERENT AMOUNT. \n");
                    sizeA = kbd.nextInt();
                }

            }
            System.out.println("\nENTER THE FIRST SET OF NUMBERS");

            getData(setA, sizeA);
            sort(setA,sizeA);


            System.out.println("\nHOW MANY NUMBERS IN YOUR SECOND SET?");
            sizeB=kbd.nextInt();
            for(int i=0; i<sizeB; i++){

                if (sizeB>MAXSIZE){
                    System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED.  \n"
                            + "ENTER A DIFFERENT AMOUNT.\n ");
                    sizeB = kbd.nextInt();
                }

            }
            System.out.println("\nENTER THE SECOND SET OF NUMBERS");

            getData(setB, sizeB);
            sort(setB, sizeB);

            System.out.println("\nINTERSECTION:");
            resultSize=intersection(setA,sizeA,setB,sizeB, resultSet);
            for(int i=0; i<resultSize; i++){
                System.out.print(resultSet[i] + " ");
            }

            System.out.println("\n\nDIFFERENCE OF A-B:");
            resultSize=difference(setA,sizeA,setB,sizeB, resultSet);
            for(int i=0; i<resultSize; i++){
                System.out.print(resultSet[i]+" ");
            }
            System.out.println("\n\nDIFFERENCE OF B-A:");
            resultSize=difference(setB,sizeB,setA,sizeA, resultSet);
            for(int i=0; i<resultSize; i++){
                System.out.print(resultSet[i]+" ");
            }
            System.out.println("\n\nDO YOU WANT TO CONTINUE?");

            repeat= kbd.next().toLowerCase();

        } while(repeat.equals("y"));

    }

}

任何帮助将不胜感激。

ENTER 2 SETS OF NUMBERS.

你的第一套中有多少个? 3

输入第一组数字 4 3 2

你的第二套中有多少个? 2

输入第二组数字 五 6

路口:

A-B的差异: 2 3 4

B-A的差异: 5 6

你想继续吗? ÿ 输入2组数字。

你的第一套中有多少个? 1

输入第一组数字 6

你的第二套中有多少个? 6

输入第二组数字 3 五 4 1 0 9 7

路口:

A-B的差异: 6

B-A的差异: 1 3 4 5 7 9

你想继续吗? 8 再见

在第二次循环中,用户数量以某种方式递增到广告1或一对

的位置

2 个答案:

答案 0 :(得分:1)

如果没有预览输出,很难说,但我会采取刺激措施。对于我来说,在do / while的第二次迭代期间,如果您使用的数组大小小于原始数组,那么您可以在第一次迭代中保留垃圾。

最好在每次迭代期间重新初始化这些变量。

do {

            System.out.println("ENTER 2 SETS OF NUMBERS.\n");
            System.out.println("HOW MANY NUMBERS IN YOUR FIRST SET?");
            sizeA=kbd.nextInt();
            while(sizeA>MAXSIZE){
                    System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED."
                            + "ENTER A DIFFERENT AMOUNT. \n");
                    sizeA = kbd.nextInt();

            }
            System.out.println("\nENTER THE FIRST SET OF NUMBERS");
            setA = new int[MAXSIZE];

            getData(setA, sizeA);
            sort(setA,sizeA);


            System.out.println("\nHOW MANY NUMBERS IN YOUR SECOND SET?");
            sizeB=kbd.nextInt();
            while(sizeB>MAXSIZE){
                    System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED.  \n"
                            + "ENTER A DIFFERENT AMOUNT.\n ");
                    sizeB = kbd.nextInt();

            }
            System.out.println("\nENTER THE SECOND SET OF NUMBERS");

            setB = new int[MAXSIZE];
            getData(setB, sizeB);
            sort(setB, sizeB);

            System.out.println("\nINTERSECTION:");


.
.
.
.


} while (repeat.equal('y'))

这可以解决您的问题吗?就像我之前说过的那样,我并不完全理解你的问题,所以如果不解决这个问题,请提供更多细节,以便我能提供帮助。

答案 1 :(得分:1)

我确信以下代码会对您有所帮助。您可以将初始化数组部分移动到while循环中,以便可以使用用户输入初始化数组的大小。

public static void main(String[] args) {
    int sizeA=0, sizeB=0;int resultSize=0;
    String repeat;

    do{        
        System.out.println("ENTER 2 SETS OF NUMBERS.\n");
        System.out.println("HOW MANY NUMBERS IN YOUR FIRST SET?");
        sizeA=kbd.nextInt();

        int [] setA = new int[sizeA ]; //Initializing Array of size that user inputs.

        for(int i=0; i<sizeA; i++){

            if (sizeA>MAXSIZE){
                System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED."
                        + "ENTER A DIFFERENT AMOUNT. \n");
                sizeA = kbd.nextInt();
            }

        }
        System.out.println("\nENTER THE FIRST SET OF NUMBERS");

        getData(setA, sizeA);
        sort(setA,sizeA);


        System.out.println("\nHOW MANY NUMBERS IN YOUR SECOND SET?");
        sizeB=kbd.nextInt();

        int [] setB = new int[sizeB]; //Initializing Array of size that user inputs.

        for(int i=0; i<sizeB; i++){

            if (sizeB>MAXSIZE){
                System.out.println("\nERROR: SET SIZE LIMIT EXCEEDED.  \n"
                        + "ENTER A DIFFERENT AMOUNT.\n ");
                sizeB = kbd.nextInt();
            }

        }
        System.out.println("\nENTER THE SECOND SET OF NUMBERS");

        getData(setB, sizeB);
        sort(setB, sizeB);

        /* Initializing the result array with Largest among sizeA and sizeB*/
        resultSize = sizeA;
        if(sizeB > sizeA)
        {
             resultSize = sizeB;
        }
        int [] resultSet = new int[resultSize];

        System.out.println("\nINTERSECTION:");
        resultSize=intersection(setA,sizeA,setB,sizeB, resultSet);
        for(int i=0; i<resultSize; i++){
            System.out.print(resultSet[i] + " ");
        }

        System.out.println("\n\nDIFFERENCE OF A-B:");
        resultSize=difference(setA,sizeA,setB,sizeB, resultSet);
        for(int i=0; i<resultSize; i++){
            System.out.print(resultSet[i]+" ");
        }
        System.out.println("\n\nDIFFERENCE OF B-A:");
        resultSize=difference(setB,sizeB,setA,sizeA, resultSet);
        for(int i=0; i<resultSize; i++){
            System.out.print(resultSet[i]+" ");
        }
        System.out.println("\n\nDO YOU WANT TO CONTINUE?");

        repeat= kbd.next().toLowerCase();

    } while(repeat.equals("y"));