【问题标题】:Logic error towards the end逻辑错误接近尾声
【发布时间】:2015-01-24 07:42:44
【问题描述】:

我正在完成一个本周到期的项目。它是一个按升序获取数组的交集和差的程序。就方法而言,一切都在工作,但我试图花哨并在用户响应之间添加一个换行符,但我一定在某个地方删除了一些东西并且无法找到我的日志到那个部分。它假设只是重复 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

输入第二组数字 5 6

交叉路口:

A-B 的区别: 2 3 4

B-A 的区别: 5 6

您要继续吗? 是 输入 2 组数字。

您的第一组有多少个数字? 1

输入第一组数字 6

第二组有多少个数字? 6

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

交叉路口:

A-B 的区别: 6

B-A 的区别: 1 3 4 5 7 9

您要继续吗? 8 再见

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

【问题讨论】:

  • 如果它是 netbeans,你使用了哪个 ide,你可以恢复你的代码
  • 请看http://stackoverflow.com/a/12579781/1262764
  • 它不工作。我没有这些选项。
  • 请为您的应用发布示例输入和所需的输出,以便对其进行调试。

标签: java arrays


【解决方案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"));

【讨论】:

  • 哇。你们解决了我的两个问题:找到我不小心擦除的内容(循环内的数组)和我的逻辑错误,这是由于在 while 循环之外初始化数组而再次将它们放入其中。这样做是不合逻辑的,因为正如@David Jones 所说,原始数组从未真正丢弃所有数字,只会将它们添加到第二次迭代数组中。这意味着在我的原始编辑中,每个集合都没有丢弃原始集合,而只是在每次迭代时使数组大小变大。感谢 David 和 Shridhar。
【解决方案2】:

如果没有预览您的输出很难说,但我会试一试。在我看来,在 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'))

这能解决您的问题吗?就像我之前说的,我不完全理解您的问题,所以如果这不能解决问题,请提供更多详细信息以便我提供帮助。

【讨论】:

  • 另外,一个简单的问题,我发现使用 while 循环执行它看起来更干净一些,但由于某种原因,我对 for 和 do while 循环感到更舒服,因为我是这个人的新手csci的东西,这两种技术之间的效率有区别吗?正如我所说,我相当新,所以请原谅我的业余问题。我认为这个领域真的很整洁,并且希望做得更好。我只需要更好地理解和分解逻辑。
  • 在您的代码中,您希望继续要求用户输入列表大小,直到该值正确为止。使用 for 循环并不能确保这是真的。想象一下 'sizeA' 或 'sizeB' 是 10,这意味着 for 循环运行了 10 次。如果用户输入了 11 个不正确的值,这将导致 sizeA 的尺寸无效。此外,for 循环取决于“sizeA”或“sizeB”的大小。您几乎不应该在 for 循环中更改此大小的值。这可能会导致一些不需要的行为。您应该阅读更多 Java 基础知识,并且可能会做一些基本的 Java 教程。
猜你喜欢
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
相关资源
最近更新 更多