[英]Convert binary to decimal with an array
Im trying to make a binary string into a decimal. 我试图将二进制字符串转换为十进制。 It will terminate if -1 is entered.
如果输入-1,它将终止。 I am stuck with using an array.
我坚持使用数组。 It was suggested to use: public static int binaryToDecimal (String binaryString) .
建议使用:public static int binaryToDecimal(String binaryString)。 But Im not sure how to do that.
但是我不确定该怎么做。 This is what I have:
这就是我所拥有的:
import java.util.Scanner;
public class BinaryConversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
while (inString != "-1") {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = 0, decimal = 0; i < binaryLength; i++) {
decimal = decimal * 2 + (inString[i] - 0);
System.out.print(decimal);
}
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
}
It says there is a compilation problem with the array. 它说数组存在编译问题。 Thank you!
谢谢!
inString is a String, not an array. inString是字符串,而不是数组。 So, you can't use inString[i].
因此,您不能使用inString [i]。 To get the character at a given position in the string, use inString.charAt(i), which returns a char.
要在字符串中的给定位置获取字符,请使用inString.charAt(i),它返回一个char。
Then, you'll also have to convert that char into an int. 然后,您还必须将该char转换为int。 You can do this with Character.getNumericValue(char).
您可以使用Character.getNumericValue(char)进行此操作。
So in summary, instead of 所以总的来说,
inString[i]
you need to use 你需要使用
Character.getNumericValue(inString.charAt(i))
Try this one: 试试这个:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
//Character.getNumericValue(inString.charAt(i))
while (inString != "-1") {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = 0, decimal = 0; i < binaryLength; i++)
{
decimal = decimal * 2 + (Character.getNumericValue(inString.charAt(i)) - 0);
System.out.print(decimal);
}
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
} }
As suggested, you have to use Character.getNumericValue
根据建议,您必须使用
Character.getNumericValue
You can simplify the code by using Integer.parseInt()
: 您可以使用
Integer.parseInt()
简化代码:
public static void main(String[] args) {
final Scanner input = new Scanner(System.in);
String inString;
while (true) {
System.out.println("Enter a binary number: ");
inString = input.nextLine();
if (inString.equals("-1"))
break;
System.out.println(Integer.parseInt(inString, 2));
}
System.out.println("All set !");
}
You had few logical and few syntax errors. 您几乎没有逻辑错误和语法错误。
This is working code : 这是工作代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inString;
int decimal;
System.out.println("Enter a binary number: ");
inString = input.nextLine();
while (!"-1".equals(inString)) {
int i;
int binaryLength;
binaryLength = inString.length();
for (i = binaryLength-1, decimal = 0; i >= 0; i--) {
if (inString.charAt(i) == '1') {
decimal += Math.pow(2, binaryLength-i-1);
}
}
System.out.println(decimal);
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
Note that comparing String cannot be done with ==
, you have to use equals
or compareTo
methods. 请注意,不能使用
==
完成比较String,您必须使用equals
或compareTo
方法。
byte[] binary = {1,1,0,1};
int decimal = 0;
for(int i=binary.length-1, j=0; i>=0; i--, j++){
decimal += binary[i]*Math.pow(2, j);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.