1 回答
TA贡献1847条经验 获得超11个赞
您binaryNum在while循环内声明,因此此变量的范围将仅在循环内,将其声明在while循环外并将 binaryNum 类型更改为 String
public class Decimaltobinary {
public static String decimalToBinary(int valueIn){
// String binaryOut = "";
// int counter = 0;
int remainder, i = 0;
String binaryNum ="";
while (valueIn != 0){
remainder = valueIn % 2;
valueIn /= 2;
binaryNum = remainder+binaryNum;
}
return binaryNum;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner keyboard = new Scanner (System.in);
System.out.println("Please enter the decimal number: ");
int valueIn = keyboard.nextInt ();
String outputOut = decimalToBinary(valueIn);
System.out.println ("The output is: " +outputOut);
}
}
添加回答
举报