我想創建二進制計算器,將整數轉換爲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);
}
}