public class PrintBinary { public static void main(String[] args) { // TODO code
ID: 3704072 • Letter: P
Question
public class PrintBinary {
public static void main(String[] args) {
// TODO code application logic here
Scanner s = new Scanner(System.in);
int x = s.nextInt();
char choice;
System.out.print("Do you want to start? (Y/N): ");
choice = s.next().charAt(0);
while (true){
if(choice == 'Y' || choice == 'y'){
System.out.print("Enter an integer and I will convert it to binary code: ");
x = s.nextInt();
System.out.println("You entered " + x + ".");
printBinary(x);
}
System.out.println("Do you want to continue? (Y/N): ");
if(choice == 'N' || choice == 'n'){
break;
}
}
}//end main method
public static void printBinary(int x){
String s = " ";
while (x>0){
int d = x % 2;
x = x/2;
if (d == 0){
s = s + "0";
}
else {
s = s + "1";
}
System.out.println(x + " in binary is " + s + ".");
}
}//end printBinary method
}//end class Print Binary
----------------------------------------------------------------
output desired
Do you want to start(Y/N): y Enter an integer and I will convert it to binary code: 16 You entered 16. 16 in binary is 10000.
Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 123 You entered 123. 123 in binary is 1111011.
Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 359 You entered 359. 359 in binary is 101100111.
Do you want to continue(Y/N): y Enter an integer and I will convert it to binary code: 1024 You entered 1024. 1024 in binary is 10000000000.
Do you want to continue(Y/N): n
Can someone help me fix this
Do you want to start? (Y/N): y Enter an integer and I will convert it to binary code: 2 You entered 2 1 in binary is 0 0 in binary is 0l Do you want to continue? ?Y/N) : Enter an integer and I will convert it to binary code: 5 You entered 5 2 in binary is L 1 in binary is 10 0 in binary is 10l Do you want to continue? ?Y/N) : Enter an integer and I will convert it to binary code: 7 You entered 3 in binary is L 1 in binary is 11 0 in binary is 1ll Do you want to continue? (Y/N) Enter an integer and I will convert it to binary code: BUILD STOPPED (total time 1 minutes 30 seconds)Explanation / Answer
PrintBinary.java
import java.util.Scanner;
public class PrintBinary {
public static void main(String[] args) {
// TODO code application logic here
Scanner s = new Scanner(System.in);
char choice;
System.out.print("Do you want to start? (Y/N): ");
choice = s.next().charAt(0);
while (true){
if(choice == 'Y' || choice == 'y'){
System.out.print("Enter an integer and I will convert it to binary code: ");
int x = s.nextInt();
System.out.println("You entered " + x + ".");
printBinary(x);
}
System.out.println("Do you want to continue? (Y/N): ");
choice = s.next().charAt(0);
if(choice == 'N' || choice == 'n'){
break;
}
}
}//end main method
public static void printBinary(int x){
int n = x;
String s = "";
do {
s = s + (x & 1);
x = x >> 1;
} while (x > 0);
String reverse = new StringBuffer(s).reverse().toString();;
s= String.format("%1$" + 8 + "s", reverse).replace(' ', '0');
System.out.println(n + " in binary is " + s + ".");
}//end printBinary method
}//end class Print Binary
Output:
Do you want to start? (Y/N): y
Enter an integer and I will convert it to binary code: 16
You entered 16.
16 in binary is 00010000.
Do you want to continue? (Y/N):
y
Enter an integer and I will convert it to binary code: 123
You entered 123.
123 in binary is 01111011.
Do you want to continue? (Y/N):
n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.