簡體   English   中英

二進制計算器分配

[英]Binary Calculator Assignment

對於本實驗,您將在十進制中輸入兩個數字並將其轉換為二進制數。 然后,您將以二進制形式添加數字並打印出結果。 輸入的所有數字將介於0和255之間(包括0和255),二進制輸出限制為8位。 這意味着兩個相加數字的總和也將限制為8位。 如果兩個數字的總和超過8位,請打印總和的前8位數字和“錯誤:溢出”消息。 您的程序應使用整數數組表示二進制數,其中數字(2 ^ 0)存儲在索引0處,兩位數(2 ^ 1)存儲在索引1處,一直存儲在索引處存儲的2 ^ 7位數7.您的程序應包括以下方法:

  • int[] convertToBinary(int b)將參數轉換為二進制值,並將其作為int數組存儲。
  • void printBin(int b[])在一行中輸出存儲在數組中的二進制數。 請注意,每個輸出0或1之間應該只有一個空格。
  • int[] addBin(int a[], int b[])添加存儲在數組中的兩個二進制數,並以新的int數組返回總和。

當我將代碼輸入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;
}

這是我在網站上的第一個答案,所以我希望它適合你的測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM