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

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

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

问题

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

主要方法:

public static void main(String args[])
{
    String inputByUserString = ""; // 玩家输入的数字
    int inputByUserInteger;
    Scanner sc = new Scanner(System.in);
    System.out.println("输入一个数字"); // 获取输入
    inputByUserString = sc.nextLine();
    inputByUserInteger = Integer.parseInt(inputByUserString);

    // ... 后续部分省略 ...
}

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

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

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

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

public int binaryToDecimalConversion(int inp)
{
    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))));
        a2++;
    }
    return a3;
}

整个 switch case 部分:

System.out.println("你想进行哪种转换?");
System.out.println("1) 十进制数到二进制数");
System.out.println("2) 二进制数到十进制数");
System.out.println("3) 十进制数到八进制数");
System.out.println("4) 八进制数到十进制数");
System.out.println("5) 十进制数到十六进制数");
System.out.println("6) 十六进制数到十进制数");
System.out.println("7) 八进制数到十六进制数");
System.out.println("8) 十六进制数到八进制数");
int choice = sc.nextInt();
switch (choice)
{
    case 1:
        // 十进制数到二进制数
        break;
    case 2:
        // 二进制数到十进制数
        int binaryToDeci = obj1.binaryToDecimalConversion(inputByUserInteger);
        System.out.println("二进制数:" + inputByUserInteger);
        System.out.println("十进制数:" + binaryToDeci);
        break;
    // ... 其他情况省略 ...
    default:
        System.out.println("无效输入");
}

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

执行时显示的错误:

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

所以,请帮助我。

英文:

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:

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

So, help me.

答案1

得分: 1

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

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

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

英文:

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.

答案2

得分: 0

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

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

就像这样:

int choice = sc.nextInt();
System.out.println("Test" + choice);
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

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

答案3

得分: 0

不要紧我只需要这样做就可以解决这个问题

public int binaryToDecimalConversion(int inp) //将二进制数转换为十进制
{
    int a1;
    int a2 = 0;
    int a3 = 0;
    while (inp > 0)
    {
        a1 = inp % 10;
        a3 = (int)(a3 + (a1 * (Math.pow(2, a2)))); //结果
        a2++;
        inp = inp / 10;
    }
    return a3;
}
英文:

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

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:

确定