+ 1
Clear this to me, this binary thing. And the strange if clause.
import java.util.Scanner; //your code goes here public class Converter { public static String toBinaryString ( int n ) { if ( n < 0 ) throw new IllegalArgumentException ( "n must be non-negative" ); // special case if ( n == 0 ) return "0" ; // build string from right to left String s = "" ; while ( n > 0 ) { if ( n % 2 == 0 ) s = '0' + s ; else s = '1' + s ; n = n / 2 ; } return s ; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinaryString(x)); } }
1 Antwort