java - 二进制计算器分配

标签 java

For this lab, you will enter two numbers in base ten and translate them to binary. You will then add the numbers in binary and print out the result. All numbers entered will be between 0 and 255, inclusive, and binary output is limited to 8 bits. This means that the sum of the two added numbers will also be limited to 8 bits. If the sum of the two numbers is more than 8 bits, please print the first 8 digits of the sum and the message "Error: overflow". Your program should represent binary numbers using integer arrays, with the ones digit (2^0) stored at index 0, the twos digit (2^1) stored at index 1, all the way up to the 2^7 digit stored at index 7. Your program should include the following methods:

  • int[] convertToBinary(int b) Translates the parameter to a binary value and returns it stored as an array of ints.
  • void printBin(int b[]) Outputs the binary number stored in the array on one line. Please note, there should be exactly one space between each output 0 or 1.
  • int[] addBin(int a[], int b[]) Adds the two binary numbers stored in the arrays, and returns the sum in a new array of ints.

将我的代码输入 CodeRunner(测试代码并根据每次测试的结果返回成绩)时,我似乎无法通过其中一项测试。这是我收到的消息:

*您正确通过了 44 项测试中的 43 项。你的分数是 97%。

失败的测试是:测试:addBin() 方法不正确:返回的数字不正确*

这是我的代码:

import java.util.Scanner;

class Main {
    public static int[] convertToBinary(int a) {
        int[] bin = {0, 0, 0, 0, 0, 0, 0, 0};
        for (int i = bin.length - 1; i >= 0; i--) {
            bin[i] = a % 2;
            a = a / 2;
        }
        return bin;
    }

    public static void printBin(int[] b) {
        int z;
        for (z = 0; z < b.length; z++) {
            System.out.print(b[z] + " ");
        }
        System.out.println();
    }

    public static int[] addBin(int[] c, int[] d) {
        int[] added = new int[8];
        int remain = 0;
        for (int x = added.length - 1; x >= 0; x--) {
            added[x] = (c[x] + d[x] + remain) % 2;
            remain = (c[x] + d[x] + remain) / 2;
        }
        if (added[0] + c[0] + d[0] == 1) {
            added[0] = 1;
        } else if ((added[0] + c[0] + d[0] == 2) || (added[0] + c[0] + d[0] == 3)) {

            System.out.println("Error: overflow");
        }
        return added;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a base ten number between 0 and 255, inclusive.");
        int num1 = scan.nextInt();
        System.out.println("Enter a base ten number between 0 and 255, inclusive.");
        int num2 = scan.nextInt();

        int[] bin;
        bin = convertToBinary(num1);
        System.out.println("First binary number:");
        printBin(bin);
        int[] bin1 = bin;

        bin = convertToBinary(num2);
        System.out.println("Second binary number:");
        printBin(bin);
        int[] bin2 = bin;


        System.out.println("Added:");
        {
            printBin(addBin(bin1, bin2));
        }
    }
}

如果有人可以看看我上面的代码,看看他们是否可以告诉我需要更改什么来修复 addbin() 方法,以便它通过所有测试,那就太好了!即使您不确定它是否有效,我们也非常感谢您的帮助!谢谢!

最佳答案

您好,首先请原谅我的英语,但我想您的作业也接受 1 和 255。所以我添加了其中两个并在您的代码中得到 1 0 0 0 0 0 0 0 。但我认为它需要是 0 0 0 0 0 0 0 0 并出现溢出错误。所以我稍微更改了您的代码。

public static int[] addBin(int[] c, int[] d) {
    int[] added = new int[8];
    int remain = 0;
    for (int x = added.length - 1; x >= 0; x--) {
        added[x] = (c[x] + d[x] + remain) % 2;
        remain = (c[x] + d[x] + remain) / 2;
    }
    if (remain!=0) {

        System.out.println("Error: overflow");
    }
    return added;
}

这是我在网站上的第一个答案,所以我希望它对你的测试有用

关于java - 二进制计算器分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374921/

相关文章:

java - spring 单元测试上下文配置

Java 8 mdmp 转储文件分析器与 visualvm 添加 VM 核心转储变灰/空白

java - Hadoop Mapreduce 自定义拆分/自定义记录读取器

Java调试器: how is the inter-process communication done?

Java - 二进制代码与字节码相同吗?

java - libgdx无法拖动com.badlogic.gdx.scenes.scene2d.ui.Window

C# 委托(delegate)到 Java 的转换

java - 还记得鼠标点击的位置吗?数组列表?哈希码?

java - 在匿名 ActionListener 中调用抛出异常的方法,而不使用 try/catch

java - 如何使用注释映射在 hibernate 中共享相同主键的两个实体?