亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在沒有數組或遞歸的情況下將十進制轉換為二進制?

如何在沒有數組或遞歸的情況下將十進制轉換為二進制?

森欄 2021-12-30 20:57:31
我試圖在沒有任何數組或沒有遞歸的情況下將十進制轉換為二進制。我知道您可以使用一個命令將十進制轉換為二進制,但我正在嘗試手動執行此操作。任何幫助,將不勝感激。   public class Decimaltobinary {    public static String decimalToBinary(int valueIn){        //    String binaryOut = "";        //    int counter = 0;        int remainder, i = 0;        while (valueIn != 0){            remainder = valueIn % 2;            valueIn /= 2;            int binaryNum += remainder * i;            i *= 10;        }        return binaryNum;    }    /**    * @param args the command line arguments    */    public static void main(String[] args) {        // TODO code application logic here        Scanner keyboard = new Scanner (System.in);        System.out.println("Please enter the decimal number: ");        int valueIn = keyboard.nextInt ();        String outputOut = decimalToBinary(valueIn);        System.out.println ("The output is: " +outputOut);        }}我收到返回 BinaryNum 語句的錯誤,顯示“找不到符號”和錯誤 int binaryNum += remainder * i;陳述。錯誤說 '; 預期的'。
查看完整描述

1 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您binaryNum在while循環內聲明,因此此變量的范圍將僅在循環內,將其聲明在while循環外并將 binaryNum 類型更改為 String


public class Decimaltobinary {


public static String decimalToBinary(int valueIn){


     //    String binaryOut = "";

     //    int counter = 0;

      int remainder, i = 0;

       String binaryNum ="";

      while (valueIn != 0){

        remainder = valueIn % 2;

        valueIn /= 2;

        binaryNum = remainder+binaryNum;

      }

     return binaryNum;

}


/**

* @param args the command line arguments

*/


public static void main(String[] args) {

    // TODO code application logic here

    Scanner keyboard = new Scanner (System.in);

    System.out.println("Please enter the decimal number: ");

    int valueIn = keyboard.nextInt ();

    String outputOut = decimalToBinary(valueIn);

    System.out.println ("The output is: " +outputOut);    

   }


}


查看完整回答
反對 回復 2021-12-30
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號