gpt4 book ai didi

java - 尝试对二进制数求和时出现 ArrayIndexOutOfBoundsException

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

//我对此很陌生,我试图在此处添加两个二进制数,但我得到了这个异常,我知道在这里进行更改可能不仅仅是一两件事才能使其正常工作,但我真的想知道我在这里做错了什么,谢谢!

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

}

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

关于java - 尝试对二进制数求和时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361957/

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