二进制计算器分配(JAVA) [英] Binary Calculator Assignment (Java)

查看:170
本文介绍了二进制计算器分配(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是为分配的要求说明:

有关本实验中,你将十进制中输入两个数字,并将其转化为二进制。然后,您将在二进制加号,并打印出结果。
输入的所有数字将是0和255之间,包括,和二进制输出被限制为8位。这意味着两个附加数字的总和也将被限制为8位。如果这两个数之和大于8位,请打印的前8位数字的总和和消息的错误:溢出。
你的程序应该使用整型数组重新present二进制数,与存储在索引0个位数(2 ^ 0),三三两两位(2 ^ 1)存储在索引1,一路攀升到2 ^ 7数字存储在索引7。
你的程序应该包括下面的方法:
INT [] convertToBinary(INT B)平移参数为二进制值,并将其存储为int数组。
空隙printBin(中间体B〔〕)输出数组中存储的一行上的二进制数。请注意,应该有每个输出0之间或者一个空格1。
INT [] addBin(int类型的[],INT B〔])将存储在阵列中的两个二进制数,并在整数的新数组返回的总和。

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.

当进入我的code到codeRunner(其中测试code和A级背取决于每个测试的结果返回),我似乎无法通过测试之一。这是我收到的消息:

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

*You had 43 out of 44 tests pass correctly. Your score is 97%.

这是失败的测试是:
测试:addBin()方法
    错误:错误号退回*

The tests that failed were: Test: addBin() method Incorrect: Incorrect number returned*

我的继承人code:

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");
}
//System.out.println(added);
//don't need to print added, but it is the only way to check it
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));
}
 }}

如果任何人都可以看看我的code以上,看看他们是否能告诉我什么需要改变以固定addbin()方法,让它通过了所有测试,即会是伟大的!任何帮助是极大AP preciated即使你不知道它的工作!谢谢!

推荐答案

您添加了错误的方向。

个位数(2 ^ 0)存储在索引0,存储在索引1两岁位(2 ^ 1),一路到存储在索引7 2 ^ 7位数

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

这意味着:

int[] num1 = {1,0,1,0,0,0,0,0}; // 5
int[] num2 = {0,1,1,0,1,0,0,0}; // 22
int[] res = addBin(num1, num2); // 27 = {1,1,0,1,1,0,0,0}

您是从末尾添加,滚动结转到左:

You are adding from the end, rolling carryovers to the left:

  {  1,0,1,0,0,0,0,0}
+ {  0,1,1,0,1,0,0,0}
= {1,0,0,0,0,1,0,0,0} // Error: overflow

这篇关于二进制计算器分配(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆