[英](java) convert a decimal number to binary without using parseint
我是java新手,我正在学习如何从二进制转换为十进制,反之亦然。 在二进制转十进制的情况下,我发现我可以使用 parseint,但是我看到了其他没有使用它的方法,所以我尝试将它们实现到我的代码中,但它对我不起作用,我得到了难住了。
我如何能够使用不同的方法来计算二进制到十进制并将其实现到我的代码中?
这是我的代码:
import java.util.Scanner;
class BinaryToDecimal {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String binaryString;
char choice;
String nextLine = "Empty";
int i = 0;
choice = 'Y';
try {
do {
System.out.print("Enter a binary number: ");
binaryString = sc.nextLine();
//Find the string count
int count = binaryString.length();
for (int j = 0; j< count; j++)
{
if (binaryString.charAt(j) != '1' && binaryString.charAt(j) != '0')
{
System.out.print("Enter a binary number: ");
binaryString = sc.nextLine();
count = binaryString.length();
j=0;
}
}
i = Integer.parseInt(binaryString);
if (i>0)
System.out.println("The decimal number is: " + Integer.parseInt(binaryString, 2));
System.out.println("Continue using the calculator? Only input Y or N");
String ln = sc.next();
if(ln.length()==1){
choice = ln.charAt(0);
}
else{
choice = 'N';
}
if (sc.hasNextLine()) {
nextLine = sc.nextLine();
}
} while (choice == 'Y');
} catch (NumberFormatException nfe) {
System.out.println("Invalid input");
}
}
}
二进制数学涉及加 1 和乘以 2。我会使用正则表达式来测试输入是否有效。 当用户在提示继续时给出除y
之外的答案时,我会使用无限循环并中断。 把它放在一起,给出了一个简化的
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter a binary number: ");
String binaryString = sc.nextLine();
// An int value consists of up to 32 0 and 1s.
if (!binaryString.matches("[01]+") || binaryString.length() > 32) {
continue;
}
int v = 0;
for (int i = 0; i < binaryString.length(); i++) {
v *= 2;
if (binaryString.charAt(i) == '1') {
v++;
}
}
System.out.println("The decimal number is: " + v);
System.out.println("Continue using the calculator? Only input Y or N");
String ln = sc.nextLine();
if (!ln.equalsIgnoreCase("Y")) {
break;
}
}
看起来你错过了你错过了我使用的默认值为 2 的基数。试试这个,让我知道会发生什么
i = Integer.parseInt(binaryString,2);
可能有更好的方法来做到这一点,但是这是我想出的解决方案。 我考虑到该数字既可以是正数也可以是负数,并为这些情况添加了检查。 我还确保在输入无效二进制数时添加例外。
public static int numberFromBinary(String binaryNumber) {
char[] array = binaryNumber.toCharArray();
boolean isNegative = false;
int result = 0;
if (array.length > 32) {
throw new NumberFormatException("An integer cannot be more than 32 bits long.");
}
if (array.length == 32) {
isNegative = array[0] == '1';
if (isNegative) {
result -= 1;
}
}
for (int i = 0; i < array.length && i != 31; i++) {
int worth = (int) Math.pow(2, i);
if (array[array.length - 1] != '1' && array[array.length - 1] != '0') {
throw new NumberFormatException("Binary bits can only be a '1' or a '0'.");
}
if (isNegative) {
if (array[array.length - 1] == '0') {
result -= worth;
}
} else {
if (array[array.length - 1] == '1') {
result += worth;
}
}
}
return result;
}
这是将二进制数的字符串表示形式转换为十进制数的解决方案,无需使用 Integer.parseInt()。 这是基于您的原始问题文本:
我如何能够使用不同的方法来计算二进制到十进制并将其实现到我的代码中?
还有您添加的评论:
我也不想使用 parseint
如果你取一个二进制数并从右到左计算,每个数字都是 2 的递增幂。
0001 = 2^0 = 1
0010 = 2^1 = 2
0100 = 2^2 = 4
1000 = 2^3 = 8
您可以遵循相同的模式:检查二进制字符串输入的每个字符位置,然后将 2 提高到某个幂,以获得该位表示的十进制值被设置为 1。下面是一段简单的代码:
'1'
进行比较这是代码:
System.out.print("enter a binary number: ");
String binaryInput = new Scanner(System.in).next();
int decimalResult = 0;
int position = 0;
for (int i = binaryInput.length() - 1; i >= 0; i--) {
if (binaryInput.charAt(i) == '1') {
decimalResult += Math.pow(2, position);
}
position++;
}
System.out.println(binaryInput + " --> " + decimalResult);
还有一些示例运行:
enter a binary number: 1111
1111 --> 15
enter a binary number: 101010101
101010101 --> 341
enter a binary number: 100000000000
100000000000 --> 2048
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.