我想创建二进制计算器,将整数转换为8位二进制输出。 我迷路了,任何帮助,将不胜感激。 这是迄今为止我的进步:二进制计算器
import java.util.Scanner;
import java.lang.Math;
public class Unit4
{
public static int convertToBinary(int baseTenIntOne)
{
int [] firstNum = new int [8];
int binary = 0;
int bvalue = 1;
for (int i = 0; i < 8; i++)
{
if (baseTenIntOne % 2 == 1)
binary += bvalue;
else
binary += 0;
bvalue *= 10;
}
System.out.println(binary);
return binary;
}
public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
int baseTenIntOne;
int baseTenIntTwo;
System.out.println("Enter a base ten number between 0 and 255, inclusive.");
baseTenIntOne = scan.nextInt();
System.out.println(baseTenIntOne);
System.out.println("Enter a base ten number between 0 and 255, inclusive.");
baseTenIntTwo = scan.nextInt();
System.out.println(baseTenIntTwo);
convertToBinary(baseTenIntOne);
}
}
2014-12-04
Alex