是否有任何算法可以打印出带有正方形的阵列?

时间:2019-05-13 13:01:07

标签: java algorithm

如何可视化在代码中使用方括号创建的数组? 例如3x3阵列:

<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>page</title>
</head>
<body>
   <?php
      if (isset($_GET['text'])) {
         echo $_GET['text'];
      }
   ?>
</body>
</html>

我尝试了一个非常简单的代码,但是这些列在其他列之下

 [  ]    [  ]    [  ]   
 [  ]    [  ]    [  ]   
 [  ]    [  ]    [  ]  

3 个答案:

答案 0 :(得分:2)

public class Main{

 public static void main(String []args){

    int Columns = 3,Rows = 3;
    for (int i=0;i<Columns;i++){

           for (int j=0;j<Rows;j++){
              System.out.print("[  ]");
              System.out.format("\t");
           }
       System.out.println(" ");
    }

 }

}

此代码将起作用。 不要使用System.out.println(),而要使用System.out.print()

供参考,请单击此链接。 https://www.onlinegdb.com/B1950yw2V

在代码中导入java.util.Formatter。

答案 1 :(得分:2)

您的最大问题是import autograd.numpy as np from autograd import grad from autograd import elementwise_grad from autograd import hessian import random class Neural_Net(object): def __init__(self, inputSize, hiddenSize, outputSize, learning_rate=0.0001, epochs=100, activation1="sigmoid", activation2="softplus"): self.inputSize = inputSize self.outputSize = outputSize self.hiddenSize = hiddenSize self.learning_rate = learning_rate self.epochs = epochs if activation1 == 'softplus': self.activation1 = softplus self.activation1_grad = softplus_grad if activation1 == 'sigmoid': self.activation1 = sigmoid self.activation1_grad = sigmoid_grad if activation1 == 'tanh': self.activation1 = np.tanh self.activation1_grad = tanh_grad if activation2 == 'softplus': self.activation2 = softplus self.activation2_grad = softplus_grad if activation2 == 'sigmoid': self.activation2 = sigmoid self.activation2_grad = sigmoid_grad if activation2 == 'tanh': self.activation2 = np.tanh self.activation2_grad = tanh_grad self.W1 = np.random.randn(self.inputSize, self.hiddenSize) self.b1 = np.ones((1, self.hiddenSize)) self.W2 = np.random.randn(self.hiddenSize, self.outputSize) self.b2 = np.ones((1, self.outputSize)) def forward_prop(self, X): self.Z1 = np.dot(X, self.W1) + self.b1 self.A1 = self.activation1(self.Z1) self.Z2 = np.dot(self.A1, self.W2) + self.b2 self.A2 = self.activation2(self.Z2) return self.A2 def back_prop(self, X, Y): self.dA2 = (self.A2 - Y)*self.activation2_grad(self.Z2) self.dA1 = (np.dot(self.dA2,self.W2.T))*self.activation1_grad(self.Z1) self.W1 -= self.learning_rate*X.T.dot(self.dA1) self.b1 -= self.learning_rate*self.dA1 self.W2 -= self.learning_rate*np.dot(self.A1.T, self.dA2) self.b2 -= self.learning_rate*self.dA2 def train(self, X, Y): self.forward_prop(X) self.back_prop(X, Y) def softplus(x): return np.log(1 + np.exp(x)) def sigmoid(x): return 1/(1+np.exp(-x)) softplus_grad = elementwise_grad(softplus) sigmoid_grad = elementwise_grad(sigmoid) tanh_grad = elementwise_grad(np.tanh) NN1 = Neural_Net(inputSize=1, hiddenSize=1, outputSize=1, epochs=10000) for epoch in range(NN1.epochs): X = np.array(([[random.randint(1, 100)]])) Y = np.square(X) A2 = NN1.forward_prop(X) print("Input: " + str(X)) print("Actual Output: " + str(Y)) print("Predicted Output: " + str(A2)) print("Loss: " + str(np.mean(np.square(Y - A2)))) print("\n") NN1.train(X, Y) 将每个字符串打印到新行。

要获得理想的效果,请尝试使用System.out.println("[ ]");,它会在不添加回车符的情况下打印每一列。

System.out.print("[  ]");

输出:

int Columns = 3;
int Rows = 3;

for (int i=0;i<Columns;i++){
    for (int j=0;j<Rows;j++){
        System.out.print("[  ]");
    }
    System.out.println(" ");
}

答案 2 :(得分:0)

检查:

        StringBuilder sb = new StringBuilder();
        int Columns = 3, Rows = 3;
        for (int i = 0; i < Columns; i++) {
            for (int j = 0; j < Rows; j++) {
                sb.append("[  ]");
                if (j == Rows - 1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        System.out.println(sb.toString());