0

I am a novice programmer trying to write a program that converts an entered Binary number into a decimal number. As far as I can tell, the math and code are right, and no compilation errors are returned, however the number that is output is NOT the correct decimal number. My code is as follows:

  String num;
  double result = 0;
  do {
  Scanner in = new Scanner(System.in);
  System.out.println("Please enter a binary number or enter 'quit' to quit: ");
  num = in.nextLine();
  int l = num.length();
  if (num.indexOf("0")==-1 || num.indexOf("1")==-1 ){
    System.out.println("You did not enter a binary number.");
  }

  for (int i = 0; i < l; i++)
{ 
  result = result + (num.charAt(i) * Math.pow(2, (l - i)));
}
System.out.println("The resulting decimal number is: " +result);
  } while (!num.equals("quit"));


  if (num.equals("quit")){
    System.out.println("You chose to exit the program.");
    return;
  }

Any Help you could give would be much appreciated. I tried making my question as clear as possible but if you have any questions I will try to answer the best I can. I have not been doing this long. All I need is for someone to look it over and hopefully find the error I made somewhere, thanks.

4

4 回答 4

2

改变

result = result + (num.charAt(i) * Math.pow(2, (l - i)));

result = result + ((num.charAt(i) - '0') * Math.pow(2, i));

或更紧凑,

result += (num.charAt(i) - '0') * Math.pow(2, i);

请记住,字符'0'与数字0不同('1'和相同1);num.charAt(i)返回字符而不是整数。


int a = '0';
int b = 0;
System.out.println(Math.pow(2, a));
System.out.println(Math.pow(2, b));

输出:

2.81474976710656E14
1.0

差别不大吗?

于 2012-10-10T00:28:09.457 回答
1

该函数String.charAt();不返回可以与位相乘的数字 0 或 1,而是返回字符“id”。您需要将 String / char 转换为数字。

String num;
  double result = 0;
  do {
  Scanner in = new Scanner(System.in);
  System.out.println("Please enter a binary number or enter 'quit' to quit: ");
  num = in.nextLine();
  int l = num.length();
  if (num.indexOf("0")==-1 || num.indexOf("1")==-1 ){
    System.out.println("You did not enter a binary number.");
  }

  for (int i = 0; i < l; i++)
{ 
  result = result + (Integer.parseInt(num.substring(i,i+1)) * Math.pow(2, (l - i)));
}
System.out.println("The resulting decimal number is: " +result);
  } while (!num.equals("quit"));


  if (num.equals("quit")){
    System.out.println("You chose to exit the program.");
    return;
  }

顺便说一句:为什么不包含 0 或 1 的字符串不是二进制数字?想想1111例如。我认为您最好检查“既不是 0 也不是 1”

if (num.indexOf("0")==-1 && num.indexOf("1")==-1 ){
    System.out.println("You did not enter a binary number.");
  }
于 2012-10-10T00:28:30.863 回答
0

请注意,它num.charAt(i)给出了位置字符的 ASCII 码i。这不是您想要的值。int在对值进行任何数学运算之前,您需要将每个字符数字转换为一个。

于 2012-10-10T00:29:47.757 回答
0

Integer.parseInt(string, base)使用“base”基数将字符串解析为整数,如果无法转换,则会引发异常。

import java.util.Scanner;

public class Convertion {

    public static void main(String[] args) {
        String num;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a binary number");
        num = in.nextLine();
        try{
              //this does the conversion
              System.out.println(Integer.parseInt(num, 2));
        } catch (NumberFormatException e){
              System.out.println("Number entered is not binary");
        }  
    }
}
于 2012-10-10T02:10:05.563 回答