[英]java binary to decimal
Im having trouble converting binary to a decimal. 我无法将二进制转换为小数。 We have to use a function for the conversion and do it by hand rather than use a predefined function.
我们必须使用一个函数进行转换,而不是使用预定义的函数。 This is what I have so far, I know it is a mess but I am stuck on how to fix it.
这是我到目前为止,我知道这是一个烂摊子,但我仍然坚持如何解决它。 Thanks!
谢谢!
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 (!"-1".equals(inString)) {
int i;
int binaryLength;
binaryLength = inString.length();
public static int binaryToDecimal (String binaryString) {
for (i = binaryLength - 1, decimal = 0; i >= 0; i--) {
if (inString.charAt(i) == '1')
decimal = decimal + Math.pow(2,inString.length() - 1 - i);
}
return (int) decimal;
}
System.out.println(decimal);
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
}
To use a function, as your assignment requires, you have to write the function outside the main method, and then include a statement that calls the function. 要使用函数,就像您的赋值所需,您必须在main方法之外编写函数,然后包含一个调用该函数的语句。 So move this above the line that says
public static void main
: 所以把它移到说明
public static void main
的行上面 :
public static int binaryToDecimal (String binaryString) {
for (i = binaryLength - 1, decimal = 0; i >= 0; i--) {
if (inString.charAt(i) == '1')
decimal = decimal + Math.pow(2,inString.length() - 1 - i);
}
return (int) decimal;
}
Also, each function or method (including main
) has its own variables that it uses, called local variables; 此外,每个函数或方法(包括
main
)都有自己的变量,称为局部变量; but the local variables that each function uses are its own separate copies. 但每个函数使用的局部变量是它自己的独立副本。 Thus, the above function won't be able to use the
binaryLength
or decimal
variabes belonging to main
. 因此,上述函数将无法使用属于
main
的binaryLength
或decimal
变量。 You'll need to declare them inside binaryToDecimal
: 你需要在
binaryToDecimal
声明它们:
public static int binaryToDecimal (String binaryString) {
int decimal;
int binaryLength;
for (i = binaryLength - 1, decimal = 0; i >= 0; i--) {
if (inString.charAt(i) == '1')
decimal = decimal + Math.pow(2,inString.length() - 1 - i);
}
return (int) decimal;
}
Also, this function won't be able to access main
's inString
, but the idea is that you've given the function the string you want to work with, which it refers to as binaryString
. 此外,此函数将无法访问
main
的inString
,但您的想法是您已为函数提供了您要使用的字符串,它将其称为binaryString
。 So change inString
to binaryString
in the function: 所以在函数
inString
更改为binaryString
:
public static int binaryToDecimal (String binaryString) {
int decimal;
int binaryLength;
for (i = binaryLength - 1, decimal = 0; i >= 0; i--) {
if (binaryString.charAt(i) == '1')
decimal = decimal + Math.pow(2,binaryString.length() - 1 - i);
}
return (int) decimal;
}
And also note that the binaryLength
and decimal
variables are totally unrelated to the variables of the same name in main
. 还要注意,
binaryLength
和decimal
变量与main
同名变量完全无关。 That means that when you assigned binaryLength
in main
, that has no effect on binaryLength
in binaryToDecimal
. 这意味着当你在
main
分配binaryLength
时,这对binaryLength
中的binaryToDecimal
没有影响。 You'll need to assign it in the function. 您需要在函数中分配它。 Change
int binaryLength;
改变
int binaryLength;
to 至
int binaryLength = binaryString.length();
Finally, in order to use the function, main
will need to call it. 最后,为了使用该功能,
main
需要调用它。 Put this in the main
function: 把它放在
main
函数中:
decimal = binaryToDecimal(inString);
When main
executes that, it will call the function and tell it to work with inString
. 当
main
执行该操作时,它将调用该函数并告诉它使用inString
。 The function will call that binaryString
, though. 但是,该函数将调用该
binaryString
。 The function will return a result, and then main
will assign that result to the variable decimal
--that means the local variable decimal
that belongs to main
, since the above statement is inside main
. 该函数将返回一个结果,然后
main
将该结果赋值给decimal
变量 - 这意味着属于main
的局部变量decimal
,因为上面的语句在main
。
I don't know if this will make your whole program work. 我不知道这是否会使你的整个程序工作。 (It should, but I'm not sure.) But I'm just trying to explain the details of how to use functions.
(它应该,但我不确定。)但我只想解释如何使用函数的细节。
The confusing part is with the Math.pow
, and its complicated arguments, where off-by-one errors are easily made. 令人困惑的部分是
Math.pow
及其复杂的参数,其中很容易出现逐个错误。
Yet, if we have a number at base 10, like 但是,如果我们在10号基数有一个数字,就像
123 123
its value is 它的价值是
(((0*10)+1)*10+2)*10+3 (((0×10)+1)* 10 + 2)×10 + 3
This looks complex, but note the easy pattern: Starting out with 0, we go through the digits. 这看起来很复杂,但请注意简单的模式:从0开始,我们遍历数字。 As long as we have another dgit, we multiply the previous result by the base and add the digit value.
只要我们有另一个dgit,我们将前一个结果乘以基数并添加数字值。 That's all!
就这样! No Math.pow, no complex index calculations.
没有Math.pow,没有复杂的索引计算。
Hence: 因此:
String s = "1010";
int value = 0;
int base = 2;
for (i=0; i < s.length(); s++) {
char c = s.charAt(i);
value = value * base;
value = value + c - '0';
}
When I cleaned up your code, it worked just fine - 当我清理你的代码时,它工作得很好 -
public static int binaryToDecimal(String binaryString) {
int binaryLength = binaryString.length();
int decimal = 0;
for (int i = binaryLength - 1; i >= 0; i--) {
if (binaryString.charAt(i) == '1') {
decimal += Math.pow(2, binaryLength - 1 - i);
}
}
return decimal;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a binary number: ");
String inString = input.nextLine();
while (!"-1".equals(inString)) {
System.out.println(binaryToDecimal(inString));
System.out.println("Enter a binary number: ");
inString = input.nextLine();
}
System.out.println("All set !");
}
Output 产量
Enter a binary number:
01
1
Enter a binary number:
10
2
Enter a binary number:
-1
All set !
Here's the function after a little clean up. 这是清理一下之后的功能。
public static int binaryToDecimal (String binaryString) {
int decimal = 0;
int base = 2;
for (int i = binaryString.length() - 1; i >= 0; i--) {
if (binaryString.charAt(i) == '1')
decimal += Math.pow(base,i);
}
return decimal;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.