Algorithms 练习1.1.11

题目为编写一段代码,打印一个二维布尔数组,*表示真, 空格表示假。含有行号和列号。

我写的代码如下,采用了3个函数,可以实现选择打出与逻辑和或逻辑。主要的问题每次输入,需要使用回车键确定输入,但是回车确定后,程序会读入\r\n字符。造成多次空循环。目前还未找到解决方法。如果有人愿意不吝赐教,不胜感激。

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Test {
    public static void booleanfunction(boolean[][] matrix, char which){
        for(int i = 2; i < 4; i++){
            for(int j = 2; j < 4; j++){
                if(which == '&')
                    matrix[i][j] = matrix[1][j] && matrix[i][1];
                if(which == '|')
                    matrix[i][j] = matrix[1][j] && matrix[i][1];
            }
        }
    }

    public static void function(char which){
        boolean[][] matrix = new boolean[4][4];
        matrix[1][2] = true;
        matrix[1][3] = false;
        matrix[2][1] = true;
        matrix[3][1] = false;
        for(int i = 0; i < 4; i++)
            StdOut.print(i);
        StdOut.println();
        if((which == '&') || (which == '|')){
            booleanfunction(matrix, which);
            for(int i = 1; i < 4; i++){
                for(int j = 0; j < 4; j++){
                    if(j == 0)
                        StdOut.print(i);
                    else
                        StdOut.print(matrix[i][j]?'*':' ');
                }
                StdOut.println();
            }

        }

        else StdOut.println("Please input \'&\' or \'|\'!");
    }

    public static void main(String[] args) {
        char which;
        do{
        StdOut.println("which boolean table?");
        which = StdIn.readChar();
        function(which);
        } while((which != '&') || (which != '|'));
    }
}

运行后结果如下图:
输入&后的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值