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

为什么我的二进制到十进制转换在java中不起作用?,java,methods,binary,decimal,Java,Methods,Binary,Decimal,我有一个方法,它将用户的二进制输入转换成十进制值 主要方法: 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"); //g

我有一个方法,它将用户的二进制输入转换成十进制值

主要方法:

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);
然后我创建了一个开关案例,其中有更多的转换选项,比如十进制数到二进制数等

然后在该开关中,我调用一个方法:

    int binaryToDecimalNumberVariable=obj1.binaryToDecimalConversion(inputByUserInteger);   
    System.out.println("Binary Number:"+inputByUserInteger);
    System.out.println("Decimal Number:"+binaryToDecimalNumberVariable);
二进制到十进制转换的方法:

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;
}
整个开关箱:

  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
它在编译时没有显示错误,但当我执行它时,它只是死机

执行时显示的错误:


因此,请帮助我。

要详细介绍我的评论,请使用正确的方法来处理您的需求:

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

最后10个是可选的,因为它是默认值。

如果它卡住了,它可能没有检测到输入,正在等待输入。为了证实这一点,我将添加一个

System.out.println("Test" + choice);
就在选项输入下面,所以是这样的:

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

如果打印不正确,则表明您发现了问题。它可能与
系统有关。在

中,没关系,我只需要这样做,它就解决了问题

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

您是否记得使用
break在您的案例中?你能展示一下整个开关盒吗?另外,为什么不使用
nextLine()
,而不是
nextLine()
?那么您就不需要转换了。您是否尝试过调试或只是插入一些
System.out.println()
,以查看方法是否被正确调用,开关大小写是否工作,等等?为什么要尝试将int从二进制转换为十进制?这毫无意义。int本身没有基数。字符串表单有一个基数,因此在从字符串转换为int以及从int转换为字符串时执行转换。在任何时候,您是否像这样关闭sc:
sc.close()
?如果你有,那可能是你问题的原因。不,那不是问题所在。在binaryToDecimalConversion方法中有一个明显的无限循环。然而,实际上并不需要这种方法。看看我的答案,删除那个转换方法,问题就解决了。