繁体   English   中英

试图汇总二进制数时出现ArrayIndexOutOfBoundsException

[英]ArrayIndexOutOfBoundsException while trying to sum up binary numbers

//我对此并不陌生,我想在这里添加两个二进制数,但是我遇到了这个异常,我知道在这里进行更改可能不仅仅需要一两件事,但我真的很想知道我在这里做错了,谢谢!

import java.util.*;
public class Problema2 {

public static void main(String[] args) {
    int size;
    int tineminte=0;
    Scanner input = new Scanner(System.in);
    System.out.println("Marimea celor doua numere ce urmeaza sa fie adunate:");
    size=input.nextInt();

    int counter=0;  
    Vector<Integer> A = new Vector<Integer>();
    System.out.println("Primul numar in forma binara:");
    while(counter<size)
    {
        A.add(counter,input.nextInt());
        counter++;
    }
    System.out.println("Primul numar in forma binara este:");
    for(counter=0;counter<size;counter++)
        System.out.println(A.elementAt(counter));


    counter=0;  





    Vector<Integer> B = new Vector<Integer>();
    System.out.println("Al doilea numar in forma binara:");
    while(counter<size)
    {
        B.add(counter,input.nextInt());
        counter++;

    }


    System.out.println("Al doilea numar in forma binara este:");
    for(counter=0;counter<size;counter++)
        System.out.println(B.elementAt(counter));



    Vector<Integer> C = new Vector<Integer>();

    counter=size-1;

    while(counter!=-1)
    {
        if(A.elementAt(counter)==1&&B.elementAt(counter)==0) 
        {   if(tineminte==1)

            C.insertElementAt(0, counter);

        else
            C.insertElementAt(1, counter);

        counter--;
        }
        else
            if(A.elementAt(counter)==0&&B.elementAt(counter)==1) 
            { if(tineminte==1){
                C.insertElementAt(0, counter);
            }
            else
                C.insertElementAt(1, counter);

            counter--;
            }
            else
                if(A.elementAt(counter)==0&&B.elementAt(counter)==0)

                {  if(tineminte==1){
                    tineminte=0;
                    C.insertElementAt(1, counter);
                }
                else 
                    C.insertElementAt(0, counter);

                counter--;
                }
                else
                    if(A.elementAt(counter)==1&&B.elementAt(counter)==1) 
                    { 

                        if(tineminte==1){C.insertElementAt(1, counter);

                        }
                        else

                            C.insertElementAt(0, counter);


                        tineminte=1;

                        counter--;
                    }

    }
    System.out.println("Suma:");        
    for(int i:C)
        System.out.println(C.elementAt(i)); 

}
} 
public static void main(String[] args) {
        int size;
        int tineminte = 0;
        Scanner input = new Scanner(System.in);
        System.out
                .println("Input number of bits:");
        size = input.nextInt();

        int counter = 0;
        Vector<Integer> A = new Vector<Integer>();
        System.out.println("Input First number in binary");
        while (counter < size) {
            A.add(counter, input.nextInt());
            counter++;
        }
        System.out.println("First number in binary (output):");
        for (counter = 0; counter < size; counter++)
            System.out.println(A.elementAt(counter));

        counter = 0;

        Vector<Integer> B = new Vector<Integer>();
        System.out.println("Input second number in binary:");
        while (counter < size) {
            B.add(counter, input.nextInt());
            counter++;

        }

        System.out.println("Second number in binary (output):");
        for (counter = 0; counter < size; counter++)
            System.out.println(B.elementAt(counter));

        Vector<Integer> C = new Vector<Integer>();

        counter = size - 1;

        while (counter != -1) {
            if (A.elementAt(counter) == 1 && B.elementAt(counter) == 0) {
                if (tineminte == 1)
                    C.add(0);

                else
                    C.add(1);

                counter--;
            } else if (A.elementAt(counter) == 0 && B.elementAt(counter) == 1) {
                if (tineminte == 1) {
                    C.add(0);
                } else
                    C.add(1);

                counter--;
            } else if (A.elementAt(counter) == 0 && B.elementAt(counter) == 0)

            {
                if (tineminte == 1) {
                    tineminte = 0;
                    C.add(1);
                } else
                    C.add(0);

                counter--;
            } else if (A.elementAt(counter) == 1 && B.elementAt(counter) == 1) {

                if (tineminte == 1) {
                    C.add(1);

                } else

                    C.add(0);

                tineminte = 1;

                counter--;
            }

        }
        // You forgot the last bit
             if (tineminte==1)
            C.add(1);
        System.out.println("Suma:");
        for(int i=C.size()-1;i>=0;i--) {
            System.out.println(C.elementAt(i));
        }

    }

注意:我尝试仅更改代码的必要部分。

暂无
暂无

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

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