gpt4 book ai didi

java - toBinaryString 的前导为 1

转载 作者:行者123 更新时间:2023-12-02 03:42:04 24 4
gpt4 key购买 nike

我在一个简单的计算器中使用 toBinaryString 将整数转换为其二进制等价物,每当进行除加法之外的任何数学运算时,我都会得到前导 1 和不应该出现的 0。

例如,我从 10 中减去 5,这应该得到二进制的 5 或 101,但我得到的是

Output: 11111111111111111111111111110001

这是我不知道的 toBinaryString 问题,还是我做错了什么?

主要代码:

    if (input == 1)
{
System.out.println("- ADDITION -");
toDo = operator.getInputs(input); // Gets how many values to input.
ArrayList<Integer> list = new ArrayList<>(toDo);
operator.addToList(list, toDo); // Adds values input to list.
operator.addBinary(list); // Add values in 'list' together.
}

if (input == 2)
{
System.out.println("- Subtraction -");
toDo = operator.getInputs(input); // Gets how many values to input.
ArrayList<Integer> list = new ArrayList<>(toDo);
operator.addToList(list, toDo); // Adds values input to list.
operator.subBinary(list); // Subtract values in 'list' for eachother.
}

if (input == 3)
{
System.out.println("- Multiplication -");
toDo = operator.getInputs(input); // Gets how many values to input.
ArrayList<Integer> list = new ArrayList<>(toDo);
operator.addToList(list, toDo); // Adds values input to list.
operator.multBinary(list); // Multiplies values in 'list' together.
}

方法:

class binaryOperations
{
private Scanner scan = new Scanner(System.in);

int getInputs(int b)
{
int input;
System.out.println("How many integers do you wish to input: ");
return input = scan.nextInt();
}

int printBinary(int b) // Number to convert
{
String foo = Integer.toBinaryString(b); // Convert input to binary
return (Integer.parseInt(foo));
}

ArrayList<Integer> addToList(ArrayList<Integer> list, int toDo)
{
for (int i = 0; i < toDo; i++)
{
if (i == 0)
System.out.println("Please enter the first integer:");
else
System.out.println("Please enter the next integer: ");

int input = scan.nextInt();
list.add(input);
}
return list;
}


// Addition //
void addBinary(ArrayList<Integer> list)
{
int temp = 0, sum = 0;
for (int i = 0; i <= list.size() - 1; i++)
{
temp = list.get(i);
sum += temp;
}

String foo = Integer.toBinaryString(sum); // convert the sum to a string

System.out.println("The sum of the numbers in binary is " + foo);
}

// Subtraction //
void subBinary(ArrayList<Integer> list)
{
int temp = 0, difference = 0;
for (int i = 0; i <= list.size() - 1; i++)
{
temp = list.get(i);
difference -= temp;
}

String foo = Integer.toBinaryString(difference); // convert the difference to a string

System.out.println("The difference of the numbers in binary is " + foo);
}

// Multiplication //
void multBinary(ArrayList<Integer> list)
{
int temp = 0, products = 0;
for (int i = 0; i <= list.size() - 1; i++)
{
temp = list.get(i);
products *= temp;
}

String foo = Integer.toBinaryString(products);

System.out.println("The products of the numbers in binary is " + foo);
}
}

最佳答案

您的 subBinary 无法正常工作。它的作用是:

0 - firstElementofList - secondElementofList ...

因此,在您的情况下,如果您的列表包含 {10,5},您将得到 -15,它由以下二进制值精确表示:

11111111 11111111 11111111 11110001

您需要做的就是更改它,可能会从第一个中减去其余部分:

void subBinary(ArrayList<Integer> list)
{
int temp = 0, difference = list.get(0);
for (int i = 1; i <= list.size() - 1; i++)
{
temp = list.get(i);
difference -= temp;
}

String foo = Integer.toBinaryString(difference); // convert the difference to a string

System.out.println("The difference of the numbers in binary is " + foo);
}

现在,如果您提供 {5,10},您将得到 -5

关于java - toBinaryString 的前导为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726825/

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