或者您可以编写这样的程序(修改它):
import java.util.Scanner;
public class DecToBinary {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int broj;
System.out.print("Enter number that you want to conver to binary: ");
broj = input.nextInt();
convert(broj);
}
public static void convert(int a) {
String obrnuti = "";
while (a > 0) {
int x = a % 2;
obrnuti += x;
a /= 2;
}
System.out.println(reverse(obrnuti));
}
public static String reverse(String a) {
String novi = "";
for (int i = a.length() - 1; i >= 0; i--) {
char c = a.charAt(i);
novi += c;
}
return novi;
}
}