+ 2
Problem with my code
Hey guys! I tried to do the Binary Converter in Java Chapter 4 but it doesnt work! Some1 knows what I did wrong? This is my code : import java.util.Scanner; public class Converter (){ public static String toBinary(int num){ String binary=""; while(num > 0) { binary = (num%2)+binary; num /= 2; } return binary; } } public class Program { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.print(Converter.toBinary(x)); } } Hope someone could help!
8 odpowiedzi
+ 2
That can be look like:
https://code.sololearn.com/cvmIh1513hv7/?ref=app
+ 1
This is also okay.
+ 1
You should write the class without ():
public class Converter {
https://code.sololearn.com/cbq12cROX96D/?ref=app
+ 1
Oh wow...yes , I didnt see that. Thank you!
+ 1
By the first reading I didnt see it too.
+ 1
You can try this maybe it helps.
import java.util.Scanner;
public class Converter {
public static String toBinary(int num)
{
String binary = "";
while (num > 0)
{
binary = (num % 2) + binary;
num /= 2;
}
return binary;
}
}
//your code goes here
public class Program {
public static void main(String[ ] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
System.out.print(Converter.toBinary(x));
}
}
0
In the task it says that you can program it with this :
String binary="";
while(num > 0) {
binary = (num%2)+binary;
num /= 2;
}
I guess this is wrong or? Or can you do it with that?
0
But I used it in my code and it wasnt right :/
I dont even get an output , you know why?