自己写了一个二进制转十进制的,能运行,但是有的二进制转换出来的不正确,帮我看下哪里逻辑错误了,谢谢啊,新手String number;
int num;
int n = 0,count = 0,m;
number = JOptionPane.showInputDialog("Enter numbers");
num = Integer.parseInt(number);
while(n<100000){
m = (int) (num/Math.pow(10, n)%10*Math.pow(2, n));
count = count + m;
n++;
}
JOptionPane.showMessageDialog(null, count,"",JOptionPane.INFORMATION_MESSAGE);
int num;
int n = 0,count = 0,m;
number = JOptionPane.showInputDialog("Enter numbers");
num = Integer.parseInt(number);
while(n<100000){
m = (int) (num/Math.pow(10, n)%10*Math.pow(2, n));
count = count + m;
n++;
}
JOptionPane.showMessageDialog(null, count,"",JOptionPane.INFORMATION_MESSAGE);
numLength = number.length();
while(n<numLength)
String number;
int num;
int count = 0;
number = JOptionPane.showInputDialog("Enter numbers");
num = Integer.parseInt(number);
int j = String.valueOf(num).length();while(i > 0) {
int n = num / (int)(Math.pow(10,j - 1));
count = n *(int)(Math.pow(2,j-1)) + count;
num = num - (int)(Math.pow(10,j - 1));
j--;
}
JOptionPane.showMessageDialog(null, count,"",JOptionPane.INFORMATION_MESSAGE);
long num,numLength;
long n = 0,count = 0,m;
number = JOptionPane.showInputDialog("Enter numbers");
num = Long.parseLong(number);
numLength = number.length();
while(n<numLength){
m = (long) (num/Math.pow(10, n)%10*Math.pow(2, n));
count = count + m;
n++;
}
JOptionPane.showMessageDialog(null, count,"",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
好像没问题,不过还是谢谢了,受教了,又多学了一种方法