为什么我的二进制转十进制的Java代码不起作用?

huangapple go评论101阅读模式
英文:

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

问题

我有这个方法,它可以将用户输入的二进制转换为十进制值。

主要方法:

  1. public static void main(String args[])
  2. {
  3. String inputByUserString = ""; // 玩家输入的数字
  4. int inputByUserInteger;
  5. Scanner sc = new Scanner(System.in);
  6. System.out.println("输入一个数字"); // 获取输入
  7. inputByUserString = sc.nextLine();
  8. inputByUserInteger = Integer.parseInt(inputByUserString);
  9. // ... 后续部分省略 ...
  10. }

然后我创建了一个包含更多转换选项的 switch case,比如从十进制数到二进制数等等...

在该 switch 中,我调用一个方法:

  1. int binaryToDecimalNumberVariable = obj1.binaryToDecimalConversion(inputByUserInteger);
  2. System.out.println("二进制数:" + inputByUserInteger);
  3. System.out.println("十进制数:" + binaryToDecimalNumberVariable);

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

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

整个 switch case 部分:

  1. System.out.println("你想进行哪种转换?");
  2. System.out.println("1) 十进制数到二进制数");
  3. System.out.println("2) 二进制数到十进制数");
  4. System.out.println("3) 十进制数到八进制数");
  5. System.out.println("4) 八进制数到十进制数");
  6. System.out.println("5) 十进制数到十六进制数");
  7. System.out.println("6) 十六进制数到十进制数");
  8. System.out.println("7) 八进制数到十六进制数");
  9. System.out.println("8) 十六进制数到八进制数");
  10. int choice = sc.nextInt();
  11. switch (choice)
  12. {
  13. case 1:
  14. // 十进制数到二进制数
  15. break;
  16. case 2:
  17. // 二进制数到十进制数
  18. int binaryToDeci = obj1.binaryToDecimalConversion(inputByUserInteger);
  19. System.out.println("二进制数:" + inputByUserInteger);
  20. System.out.println("十进制数:" + binaryToDeci);
  21. break;
  22. // ... 其他情况省略 ...
  23. default:
  24. System.out.println("无效输入");
  25. }

编译时没有显示错误,但在执行时却卡住了。

执行时显示的错误:

(错误截图在此处插入,由于纯文本环境无法显示图片。)

所以,请帮助我。

英文:

I have this method where it converts a binary input from a user to decimal value.

Main Method:

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

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:

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

Method for binary to decimal conversion:

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

The whole switch case:

  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

It shows no error while compiling but when I execute it,it just stucks.

Error that is showing when I execute it:

为什么我的二进制转十进制的Java代码不起作用?

So, help me.

答案1

得分: 1

为了扩展我的评论,处理您的需求的正确方法如下:

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

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

英文:

To expand on my comment, the correct way to handle your requirements:

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

The last 10 is optional, since it is the default.

答案2

得分: 0

如果它卡住了,很可能没有检测到输入,正在等待输入。要确认这一点,我会在输入选项下方添加:

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

就像这样:

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

如果打印出来不正确,那么你就找到了问题所在。这可能与System.in有关。

英文:

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

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

just under the choice input, so it would be like this:

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

If it doesn't print correctly, then you have found your issue. It may have something to do with the System.in

答案3

得分: 0

  1. 不要紧我只需要这样做就可以解决这个问题
  2. public int binaryToDecimalConversion(int inp) //将二进制数转换为十进制
  3. {
  4. int a1;
  5. int a2 = 0;
  6. int a3 = 0;
  7. while (inp > 0)
  8. {
  9. a1 = inp % 10;
  10. a3 = (int)(a3 + (a1 * (Math.pow(2, a2)))); //结果
  11. a2++;
  12. inp = inp / 10;
  13. }
  14. return a3;
  15. }
英文:

Never mind,I just have to do this and it solves the problem.

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

huangapple
  • 本文由 发表于 2020年8月18日 23:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63471502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定