Java解决十进制转换二进制,帮忙挑错,多谢啦~~~
主要是倒数第二段的问题,不管什么数输进去,小数部分都是0,所以帮忙改下吧~多谢啦~importjava.util.Scanner;publicclassTest{publ...
主要是倒数第二段的问题,不管什么数输进去,小数部分都是0,所以帮忙改下吧~多谢啦~
import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.print("请输入一个十进制数:");
Scanner keyboard = new Scanner(System.in);
double data = keyboard.nextDouble();
int intData = (int)data;
double decimalData = data - intData;
String intPart = "";
String decimalPart = "" ;
while(intData!=0) {
intPart = intPart + intData%2;
intData = intData/2;
}
String intStr = "";
for(int i = intPart.length() - 1;i >= 0;i --)
intStr += intPart.charAt(i);
String decimalStr = "";
for(int n = 0;n < 5;n++){
double temp = decimalData * 2;
temp = temp - (int)temp;
decimalStr +=(int)temp;
}
System.out.print("转化后的二进制为:" + intStr +"."+ decimalStr);
}
} 展开
import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.print("请输入一个十进制数:");
Scanner keyboard = new Scanner(System.in);
double data = keyboard.nextDouble();
int intData = (int)data;
double decimalData = data - intData;
String intPart = "";
String decimalPart = "" ;
while(intData!=0) {
intPart = intPart + intData%2;
intData = intData/2;
}
String intStr = "";
for(int i = intPart.length() - 1;i >= 0;i --)
intStr += intPart.charAt(i);
String decimalStr = "";
for(int n = 0;n < 5;n++){
double temp = decimalData * 2;
temp = temp - (int)temp;
decimalStr +=(int)temp;
}
System.out.print("转化后的二进制为:" + intStr +"."+ decimalStr);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询