Java程序设计之九个正面和背面

Java程序设计之九个正面和背面

  • 题目叙述
  • 代码展示

题目叙述

Java程序设计之九个正面和背面_第1张图片

代码展示

import java.util.Scanner;

public class ch08Q11 {
    public static void main(String[] args) {
        char[][] coin = new char[3][3];
        char[] num = new char[9];
        System.out.print("Enter a number between 0 and 511: ");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        for (int i = 8; i >= 0; i--) {
            if (input % 2 == 1) {
                num[i] = '1';
            } else num[i] = '0';
            input /= 2;
        }
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                if (num[(col + row * 3)] == '0') {
                    coin[row][col] = 'H';
                } else if (num[(col + row * 3)] == '1') {
                    coin[row][col] = 'T';
                }
            }
        }
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                System.out.print(coin[row][col] + " ");
            }
            System.out.println();
        }
    }
}

你可能感兴趣的:(java,开发语言,后端,intellij-idea)