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

b4lqfgs4  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(525)

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

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

然后我创建了一个开关的情况下,有更多的选择,如十进制数转换为二进制数等。。。
然后在这个开关中,我调用一个方法:

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

二进制到十进制转换的方法:

  1. public int binaryToDecimalConversion(int inp) //to convert binary number into decimal number
  2. {
  3. int a1;int a2=0;int a3 = 0;
  4. while(inp>0)
  5. {
  6. a1=inp%10;
  7. a1=inp/10;
  8. a3=(int)(a3+(a1*(Math.pow(2,a2)))); //result
  9. a2++;
  10. }
  11. return a3;
  12. }

整个开关箱:

  1. System.out.println("What type of conversion do you want?");
  2. System.out.println("1)Decimal number to Binary number");
  3. System.out.println("2)Binary number to Decimal number");
  4. System.out.println("3)Decimal number to Octal number");
  5. System.out.println("4)Octal number to Decimal number");
  6. System.out.println("5)Decimal number to Hexadecimal number");
  7. System.out.println("6)Hexadecimal number to Decimal number");
  8. System.out.println("7)Octal number to Hexadecimal number");
  9. System.out.println("8)Hexadecimal number to Octal number");
  10. int choice=sc.nextInt();
  11. switch(choice)
  12. {
  13. case 1: //Decimal number to Binary number
  14. break;
  15. case 2: //Binary number to Decimal number
  16. int binaryToDeci=obj1.binaryToDecimalConversion(inputByUserInteger);
  17. System.out.println("Binary Number:"+inputByUserInteger);
  18. System.out.println("Decimal Number:"+binaryToDeci);
  19. break;
  20. case 3: //Decimal number to Octal number
  21. break;
  22. case 4: //Octal number to Decimal number
  23. break;
  24. case 5: //Decimal number to Hexadecimal number
  25. break;
  26. case 6: //Hexadecimal number to Decimal number
  27. break;
  28. case 7: //Octal number to Hexadecimal number
  29. break;
  30. case 8: //Hexadecimal number to Octal number
  31. break;
  32. default:
  33. System.out.println("Invalid Input");
  34. } //switch close

它在编译时没有显示错误,但是当我执行它时,它就停止了。
执行时显示的错误:

所以,帮帮我。

kokeuurv

kokeuurv1#

为了进一步说明我的意见,正确的方法来处理您的需求:

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

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

zzoitvuj

zzoitvuj2#

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

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

就在选项输入下面,所以是这样的:

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

如果打印不正确,则说明您发现了问题。这可能和 System.in

wgxvkvu9

wgxvkvu93#

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

  1. public int binaryToDecimalConversion(int inp) //to convert binary number into decimal
  2. {
  3. int a1;int a2=0;int a3 = 0;
  4. while(inp>0)
  5. {
  6. a1=inp%10;
  7. a3=(int)(a3+(a1*(Math.pow(2,a2)))); //result
  8. a2++;
  9. inp=inp/10;
  10. }
  11. return a3;
  12. }

相关问题