![](/img/trans.png)
[英](java) convert a decimal number to binary without using parseint
[英]Changing binary to decimal using created constants (not parseINT() ) and declaring and initializing each of the 8 bits of the number
我現在一直在為此工作。 我需要創建常量,然后聲明並初始化每個常量,然后再將其乘以其位置值(128、64、32、16、8、4、2、1)
我知道Integer.parseINT(Binary ....)會很快完成這項工作,但是我不能使用它。
接下來是我到目前為止的情況,我是否走在正確的軌道上? 我知道我需要獲取每個單獨的用戶輸入值,並將它們乘以它們的位置值,然后相加來創建小數點,但是我一生都無法弄清楚如何通過for循環或定義每個變量
import java.util.Scanner;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.lang.Integer;
import javax.swing.JComboBox;
public class Binary
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Binary Number Please");
Integer[] digits = getDigits(keyboard.nextInt());
System.out.println(Arrays.toString(digits));
}//End bracket method main
public static Integer[] getDigits(int num)
{
List <Integer> digits = new ArrayList <Integer>();
collectDigits(num, digits);
return digits.toArray(new Integer[]{});
}//end bracket method Integer
private static void collectDigits(int num, List <Integer> digits)
{
if(num / 10 > 0)
{
collectDigits(num / 10, digits);
}//End method collectDigits
digits.add(num % 10);
}
}
我想分離用戶輸入二進制文件的每個字符,以便可以將它們乘以它們的位置值,然后添加每個乘積以創建總和,然后將其通過System.out.println()
顯示給用戶。
您正在導入錯誤的列表類型。 更改此行:
import java.awt.List;
至:
import java.util.List;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.