0

だから私は2進数から10進数などへの変換をしなければならないプロジェクトをやっています。これはこれまでの私のコードであり、バグがあります。1101などの2進数では、出てくるはずの10進数は13ですが、コードから出てくる数は11です。このバグは、1の束で始まり、単一の0のように始まるすべての2進数に発生します。

import java.util.*; // imports everything in java.util

public class newCalculator{

   * Conversion asks the user for a binary number.  It converts the input to a decimal number
   * using arrays and then displays the answer to the screen.
  */

  public static void main (String[]args){ // creates the main method
  binaryToDecimal(); //calls the user defined method binaryToDecimal
  }

  public static void binaryToDecimal() {
    Scanner scan = new Scanner(System.in); // Creates a new Scanner
    System.out.println("Input a Binary Number"); // Asks the user to input their number
    String binary = scan.next(); // Creates a new String that stores the value of the input
    char[] charArray = binary.toCharArray(); //Create a new Array and implements in the input
    double answer = 0;  // Creates a new double called answer setting it to zero
    for (double index = 0; index < charArray.length; index++){//For loop
      if (charArray[(int)index] == '1') {//If statement that allows the binary input to work
        answer = answer + Math.pow(2.0, index);//Sets the answer with the math class power of 2
      }
    }
    System.out.println(answer);//Prints out the final conversion result
     /* Test Cases   Expected Result   Output
   * 101                 5             5
   * 11                  3             3
   * 1                   1             1
   * 1101                13            11<--
   * 111                 7             7
   * 1000001             65            65
   * 1111                15            15
   * 1001                9             9
   * 11101               29            23<--
   * 10101               21            21
   * 
   */
  }

}
4

5 に答える 5

1

期待される結果は、バイナリ文字列が右から左に読み取られるかのように計算されています。ただし、コードはバイナリ文字列を左から右に読み取っています。

これを変える:

for (double index = 0; index < charArray.length; index++){

これに:

for (double index = charArray.length - 1; index >= 0; index--) {

また、次のように、インデックスとして整数を使用するように変更する必要があります。

for (int index = charArray.length - 1; index >= 0; index--) {
于 2013-02-13T23:31:09.677 に答える
0

対称性がその答えです。すべてのテスト入力を調べると、それらはすべて対称的です。 1101

あなたのアルゴリズムは正しいですが、使用する必要が ある代わりindexに、以下が正しい実装です(実際には小さな増分変更のみ)Math.powMath.pow(2.0, charArray.length - i - 1)

import java.util.Scanner;

public class NewCalc {

    public static void main(String[] args) {
        binaryToDecimal();
    }

    public static void binaryToDecimal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Input a Binary Number");
        String binary = scan.next();
        char[] charArray = binary.toCharArray();
        double answer = 0;
        for (int i = 0; i < charArray.length; i++) {
            answer = charArray[i] == '1'
                    ? answer + Math.pow(2.0, charArray.length - i - 1)
                    : answer;
        }
        System.out.println(answer);
    }
}
于 2013-02-13T23:43:46.350 に答える
0

あなたは間違った順序であなたのビットを通過しています。最初のインデックスは、最下位ビットではなく、最上位ビットを参照します。

Math.powへの呼び出しは

answer = answer + Math.pow(2.0, (charArray.length - index - 1));

また、Tom Leeseがすでに指摘しているように、ループインデックスには整数を使用してください。

于 2013-02-13T23:31:31.857 に答える
0
package pdaproject;
import java.util.Scanner;
public class NewCalc {

    public static void main(String[] args) {
        binaryToDecimal();
    } 
    // convert to decimal (base 10)
    public static void binaryToDecimal() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Input a Binary Number");
        String binary = scan.next();
        int answer = 0;
        // process left to right
        for (int i = 0; i < binary.length(); i++) {
            answer = 2 * answer + (binary.charAt(i) == '1' ? 1 : 0);
        }
        System.out.println(answer);
    }
}
于 2013-06-29T02:44:55.067 に答える
0
 public class BinaryToDecimal {
    static int testcase1=1001;
    public static void main(String[] args) {
        BinaryToDecimal test = new BinaryToDecimal();
        int result = test.convertBinaryToDecimal(testcase1);
        System.out.println(result);
    }

    //write your code here
    public int convertBinaryToDecimal(int binary)
    {
        int deci = 0;
        int p=1;
        int rem = 0;
        while(binary>0)
        {
            rem = binary%10;
            deci = deci+(rem*p);
            p = p*2;
            binary = binary/10;
        }

        return deci;
    }

}
于 2013-11-08T19:20:43.410 に答える