简体   繁体   English

为什么我的二进制到十进制的转换在 java 中不起作用?

[英]Why my binary to decimal conversion is not working in java?

I have this method where it converts a binary input from a user to decimal value.我有这种方法,它将用户的二进制输入转换为十进制值。

Main Method:主要方法:

public static void main(String args[])
{
String inputByUserString=""; //number input by the player
int inputByUserInteger;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number"); //getting the input
inputByUserString=sc.nextLine();
inputByUserInteger=Integer.parseInt(inputByUserString);

and then I create a switch case where there is more options for conversion like Decimal number to Binary number etc...然后我创建一个开关盒,其中有更多转换选项,如十进制数到二进制数等...

Then in that switch I call a method:然后在那个开关中我调用了一个方法:

    int binaryToDecimalNumberVariable=obj1.binaryToDecimalConversion(inputByUserInteger);   
    System.out.println("Binary Number:"+inputByUserInteger);
    System.out.println("Decimal Number:"+binaryToDecimalNumberVariable);

Method for binary to decimal conversion:二进制转十进制的方法:

public int binaryToDecimalConversion(int inp) //to convert binary number into decimal number
{
int a1;int a2=0;int a3 = 0;
while(inp>0)
{
    a1=inp%10;
    a1=inp/10;
    a3=(int)(a3+(a1*(Math.pow(2,a2)))); //result
    a2++;   
}
return a3;
}

The whole switch case:整个开关盒:

  System.out.println("What type of conversion do you want?");
  System.out.println("1)Decimal number to Binary number");
  System.out.println("2)Binary number to Decimal number");
  System.out.println("3)Decimal number to Octal number");
  System.out.println("4)Octal number to Decimal number");
  System.out.println("5)Decimal number to Hexadecimal number");
  System.out.println("6)Hexadecimal number to Decimal number");
  System.out.println("7)Octal number to Hexadecimal number");
  System.out.println("8)Hexadecimal number to Octal number");
  int choice=sc.nextInt();
  switch(choice)
  {
  case 1: //Decimal number to Binary number
  break;
  case 2: //Binary number to Decimal number
    int binaryToDeci=obj1.binaryToDecimalConversion(inputByUserInteger);    
    System.out.println("Binary Number:"+inputByUserInteger);
    System.out.println("Decimal Number:"+binaryToDeci);
break;
case 3: //Decimal number to Octal number
break;
case 4: //Octal number to Decimal number
break;
case 5: //Decimal number to Hexadecimal number
break;
case 6: //Hexadecimal number to Decimal number
break;
case 7: //Octal number to Hexadecimal number
break;
case 8: //Hexadecimal number to Octal number
break;
default:
System.out.println("Invalid Input");
} //switch close

It shows no error while compiling but when I execute it,it just stucks.它在编译时没有显示错误,但是当我执行它时,它只是卡住了。

Error that is showing when I execute it:执行时显示的错误:

So, help me.所以,帮帮我。

To expand on my comment, the correct way to handle your requirements:扩展我的评论,处理您的要求的正确方法:

String myBinaryNr = sc.next();
int myNr = Integer.parseInt(myBinaryNr, 2);
String myDecimalNr = Integer.toString(myNr, 10);

The last 10 is optional, since it is the default.最后 10 个是可选的,因为它是默认值。

If it's getting stuck, it probably hasn't detected an input and it's awaiting for it.如果它被卡住了,它可能没有检测到输入并且正在等待它。 To confirm this, I would add a为了确认这一点,我会添加一个

System.out.println("Test" + choice);

just under the choice input, so it would be like this:就在选择输入下,所以它会是这样的:

int choice = sc.nextInt();
System.out.println("Test" + choice);
switch(choice){...}

If it doesn't print correctly, then you have found your issue.如果打印不正确,那么您已经找到了问题所在。 It may have something to do with the System.in它可能与System.in有关

Never mind,I just have to do this and it solves the problem.没关系,我只需要这样做就可以解决问题。

public int binaryToDecimalConversion(int inp) //to convert binary number into decimal 
{
int a1;int a2=0;int a3 = 0;
while(inp>0)
{
a1=inp%10;
a3=(int)(a3+(a1*(Math.pow(2,a2)))); //result
a2++;   
inp=inp/10;
}
return a3;
}

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

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