Question

You may not understand what I wrote clearly because English is not my first language.

Anyway, here is what I wrote.

public class Exercises7point11 {

public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    int[][] binaryNumber = {{0,0,0},{0,0,0},{0,0,0}};

    System.out.print("Enter a number between 0 and 511: ");
    int decimalNumber = input.nextInt();
    int subtractNumber = 256, number = decimalNumber;

    for (int row = 0 ; row < 3; row++){
        for (int column = 0 ; column < 3; column++) {
            if(number >= subtractNumber) {
                binaryNumber[row][column] = 1;
                number = number - subtractNumber;
            }
            else {
                subtractNumber = subtractNumber / 2;
                binaryNumber[row][column] = 0;
                }

        }
    }


    // print
    for (int row = 0; row < binaryNumber.length; row++){
        for (int column = 0; column < binaryNumber[row].length; column++){
            if (binaryNumber[row][column] == 1)
            System.out.print("T ");
            else if (binaryNumber[row][column] == 0)
            System.out.print("H ");

            if (column == 2)
                System.out.print("\n");
        }
    }

}

Here is the details. Nine coins are placed in a 3-by-3 matrix with some face up and some face down. You can represent the state of the coins using a 3-by-3 matrix with values 0 (heads) and 1 (tails). Such as,

1 0 0

0 1 0

1 1 0.

There are a total of 512 possibilities, so I can use decimal numbers 0, 1, 2, 3,..., 511 to represent all states of the matrix. Write a program that prompts the user to enter a number between 0 and 511 and displays the corresponding matrix with the characters H and T.

My problem is "subtractNumber = subtractNumber / 2; binaryNumber[row][column] = 0;" in the 18 and 19 lines. Even though 'number is greater than or equal to subtractNumber, 18 and 19 lines are read.

I don't know how I can fix it.

Thank you so much!!

Was it helpful?

Solution

The output is a 3x3 matrix, but that doesn't mean you need to use a 2d array.

Here's a simpler approach. We're turning the user's input into a binary string using the toBinaryString method, then translating the binary string into H/T.

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a number between 0 and 511: ");
    int input = sc.nextInt();

    // Turn input to binary string
    String binary = Integer.toBinaryString(input);

    // Add enough zeros in front so that the string has 9 characters
    binary = binary.format("%09d", Integer.parseInt(binary));

    // Iterate through binary string one char at a time
    for (int i = 1; i < 10; i++) {
        if ('0' == binary.charAt(i - 1)) {
            System.out.print("H ");
        } else {
            System.out.print("T ");
        }

        // New line after 3 letters
        if (i % 3 == 0) {
            System.out.println();
        }
    }
}

Example output

12 in binary is 000001100

Enter a number between 0 and 511: 12
H H H 
H H T 
T H H 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top