2013-12-20 4 views
1

숫자를 2 진수로 변환하는 프로그램을 작성했으며 입력이 0011 인 경우 잘못된 출력을 제공합니다. 0011의 경우 0011이 3이어야하지만 그렇지 않으면 9를 제공합니다. 그것은 다른 입력에 대해 정확합니다.프로그램 Java 코드에서 이진수의 2 진수로 변환

코드 :

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; 
} 
} 

답변

1

이 같은 문자열을 수행

0로 시작하는 문자 그대로의 정수는, 즉 진법으로 간주됩니다
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"); 
    } 
} 
5

. 베이스 제

0011 

9.

0111 

같다

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

가 될 것이다

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

동일

(73)

0b0011은 2 진수의 정수 리터럴 표현입니다.

+0

좋은 설명을보십시오. – Aashray

0

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; 
} 
} 
0

을 시도하거나 정확히 잘못 어디로 갔는지 그에게이 또한 하나

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); 
    } 
     } 
관련 문제