简体   繁体   English

Java代码中二进制到十进制的程序给出错误

[英]A program Binary to Decimal giving error in Java code

I wrote a program to convert a number From Binary to Decimal and it is giving wrong output if the input is 0011. For 0011 answer should be 3 but it is giving 9, otherwise it is correct for other input. 我编写了一个程序,将数字从Binary转换为Decimal,如果输入为0011,则给出错误的输出。对于0011,答案应该为3,但给出的为9,否则对于其他输入是正确的。

Code: 码:

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 powerOfTwo=1,decimal=0;
    while(binary>0)
    {
        decimal+=(binary%10)*powerOfTwo;
        binary/=10;
        powerOfTwo*=2;
    }
    return decimal;
}
}

An integer literal that starts with a 0 is considered to be an octal, ie. 0开头的整数文字被视为八进制,即。 base 8. 基数8。

0011 

is

(0 * 8^3) + (0 * 8^2) + (1 * 8^1) + (1 * 8^0)

which is equal to 9. 等于9

0111 

would be 将会

(0 * 8^3) + (1 * 8^2) + (1 * 8^1) + (1 * 8^0)

equal to 73. 等于73。

You're looking for 0b0011 which is the integer literal representation of a binary number. 您正在寻找0b0011 ,它是二进制数字的整数文字表示形式。

Do it with a String like this: 用这样的字符串来做:

public static void main(String[] args) throws IOException 
{  
    boolean correctInput = true;
    BufferedReader m_bufRead = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Bitte geben sie eine Dualzahl ein:");
    String input = m_bufRead.readLine().trim();
    for(int i = 0; i < input.length(); i++) {
        if(input.charAt(i)!='0' && input.charAt(i)!='1') {
            correctInput = false;
        }
    }
    if(correctInput) {
        long dezimal = 0;
        for(int i = 0; i < input.length(); i++) {
            dezimal += Character.getNumericValue(input.charAt(i)) * Math.pow(2, input.length()-(i+1));
        }
        System.out.println("\nDezimalwert:\n" + dezimal);
    }
    else {
        System.out.println("Ihre Eingabe kann nicht umgewandelt werden");
    }
}

Try it 试试吧

public class Binary2Decimal
{
public static void main(String arg[])
{
    int sum=0,len,e, d, c;
    int a[]=new int[arg.length];

    for(int i=0; i<=(arg.length-1); i++)
    {
        a[i]=Integer.parseInt(arg[i]);        // Enter every digits i.e 0 or 1, with space 
    }
    len=a.length;
    d=len;
    for(int j=0; j<d; j++)
    {
        len--;
        e=pow(2,len);
        System.out.println(e);
        c=a[j]*e;
        sum=sum+c;
    }
    System.out.println("sum is " +sum);
}

static int pow(int c, int d)
{       
            int n=1;
    for(int i=0;i<d;i++)
    {
      n=c*n;
    }
    return n;
}
 }

Or Try This One Also 或者也尝试这个

import java.lang.*;
import java.io.*;

public class BinaryToDecimal{
 public static void main(String[] args) throws IOException{
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter the Binary value: ");
  String str = bf.readLine();
  long num = Long.parseLong(str);
  long rem;
  while(num > 0){
  rem = num % 10;
  num = num / 10;
  if(rem != 0 && rem != 1){
  System.out.println("This is not a binary number.");
  System.out.println("Please try once again.");
  System.exit(0);
  }
  }
  int i= Integer.parseInt(str,2);
  System.out.println("Decimal:="+ i);
  }
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM