Una ayuda con java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Una ayuda con java

Hola no entiendo en donde podria fallar tal vez en el tamaño del dato pero nose , solo estoy indagando si alguien me me explica es del moduli de oop java el ultimo proyecto me sale 4 bien y 1 error pero no se donde podria estar https://code.sololearn.com/cg5oJ1uTk56r/?ref=app

11th Mar 2021, 7:05 PM
Jose G8
Jose G8 - avatar
11 Answers
+ 2
No estoy seguro pero creo que el problema está en la línea 14. Dada la forma en que se está obteniendo el número en binario quizá sea mejor que la función retorne un String y no un int. Tampoco entiendo bien el if que usas en tu función, ¿es para limitar el valor máximo de x? si es así es probable que el sistema esté probando tu código con un valor más grande aún. Prueba así: static public String toBinary (int i){ int num = i; String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } return binary; }
12th Mar 2021, 3:12 AM
Diego de la Fuente Curaqueo
Diego de la Fuente Curaqueo - avatar
+ 2
Por que no usas sc.nextInt(), sc.nextLong()... En vez de... sc.nextLine() Sería más lógico desde el punto de vista de diseño de software pasar el tipo de dato correcto en vez de String Y la implementación de la función creo que es la que te pone Diego de la Fuente Curaqueo pero con particularidades public static String toBinary (int i){ // assume this bits quantity final int qbits = Integer.SIZE; StringBuilder stb = new StringBuilder(qbits); int v = i; // use 2-modulus division algorithm while (v > 0) { stb.insert(0, v % 2); v /= 2; } // fill at begining until type size stb.insert(0, "0".repeat(Math.max(0, qbits - stb.length())) ); return stb.toString(); }
13th Mar 2021, 1:09 AM
David Ordás
David Ordás - avatar
+ 1
Gracias si ayer lo resolvi , pero todo dentro del main asi me lo dieron con int x , claro lo cambie a String yo pense que no debia cambiar lo que ya me dan hecho , igualmente gracias
12th Mar 2021, 9:42 AM
Jose G8
Jose G8 - avatar
+ 1
ok .parse method expects different format: Integer.parseInt( "-1111", 2)
13th Mar 2021, 1:45 PM
zemiak
+ 1
also this returns math format -1111 Integer.toString(-15, 2)
13th Mar 2021, 5:44 PM
zemiak
0
it doesn't work with negative numbers, compare with Integer.toBinaryString(x)
12th Mar 2021, 3:22 PM
zemiak
0
zemiak to represent negative numbers you need first know how many bytes you have to represent it: 16, 32, 64... and if its negative fill left with zeros except first that is one to represent signum
13th Mar 2021, 1:14 AM
David Ordás
David Ordás - avatar
0
David Ordás , not in Java, eg this is -15 11111111111111111111111111110001
13th Mar 2021, 6:12 AM
zemiak
0
zemiak are you sure???? for represent a short you need 16 chars/bits, for int 32, for long 64... https://code.sololearn.com/cP4y568u3dwh/?ref=app
13th Mar 2021, 11:12 AM
David Ordás
David Ordás - avatar
13th Mar 2021, 2:42 PM
David Ordás
David Ordás - avatar
0
Thanks zemiak another thing that I learn but a bit confused between "valueOf/parseInt" vs "parseUnsignedInt" as you can see in my playground (I update it)
13th Mar 2021, 2:49 PM
David Ordás
David Ordás - avatar