有 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时(CodeRunner会测试代码并根据每次测试的结果返回分数),我似乎无法通过其中一项测试。这是我得到的信息:

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

失败的测试是:Test: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) 个答案

  1. # 1 楼答案

    首先请原谅我的英语,但我想你的作业也接受1和255。所以我添加了两个,在代码中得到了10。但我认为它必须是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;
    }
    

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