[英]Binary to Decimal Conversion Java Code Bug
因此,我正在做一个项目,必须将二进制数字转换为小数等。这是到目前为止的代码,并且存在错误。 在二进制数(例如1101)中,假设出现的十进制数是13,但是从代码中出来的数字是11。此错误发生在所有以一堆1开头且像一个0的二进制数上。
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
*
*/
}
}
正在计算您的预期结果,就像从右到左读取二进制字符串一样; 但是,您的代码正在从左到右读取二进制字符串。
更改此:
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--) {
您正在按错误的顺序进行操作。 您的第一个索引指的是最高有效位,而不是最低有效位。
您给Math.pow的电话应该是
answer = answer + Math.pow(2.0, (charArray.length - index - 1));
正如汤姆·里斯(Tom Leese)所指出的那样,请为循环索引使用整数。
对称性就是答案。 如果您查看所有测试输入,除了1101
之外,所有输入都是对称的
您的算法是正确的,除了需要使用Math.pow(2.0, charArray.length - i - 1)
而不是Math.pow
的index
,以下是正确的实现(实际上只有很小的增量变化)
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);
}
}
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);
}
}
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.