/* This program converts decimal to binary */
import javax.swing.JOptionPane;
public class BinaryLoop {
public static void main(String []args) {
String askForDecimal = JOptionPane.showInputDialog("Enter the decimal number you would like to convert?");
int decimalNumber = Integer.parseInt(askForDecimal);
int remainder = 0;
for (int i = 1; decimalNumber > 0; i++) {
decimalNumber /= 2;
remainder = decimalNumber % 2;
System.out.print(remainder);
}
}
}
例如,我输入 15,但它返回 1110,应该是 1111。ps,结果将从右到左读取。