Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以矩阵形式显示我的输出_Java_Netbeans - Fatal编程技术网

Java 以矩阵形式显示我的输出

Java 以矩阵形式显示我的输出,java,netbeans,Java,Netbeans,上面是我的应用程序的图像。我正在构建一个应用程序来对齐两个字符串。两个字符串将进行比较,并填充一个2d数组。2d数组的值将是矩阵的值。所有工作都非常好。唯一的问题是,当它打印矩阵的值时,我希望它以矩阵格式如下面的示例所示,而不是输出为“00000000 11011000002100013”,我希望它显示如下 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 2 1 0 1 0 1 3 打印时,必须查看打印的字符数是否等于我的行大小,然后移动到

上面是我的应用程序的图像。我正在构建一个应用程序来对齐两个字符串。两个字符串将进行比较,并填充一个2d数组。2d数组的值将是矩阵的值。所有工作都非常好。唯一的问题是,当它打印矩阵的值时,我希望它以矩阵格式如下面的示例所示,而不是输出为“00000000 11011000002100013”,我希望它显示如下

   0 0 0 0 0
   0 0 1 0 0
   0 0 1 0 0
   0 1 0 2 1
   0 1 0 1 3
打印时,必须查看打印的字符数是否等于我的行大小,然后移动到下一行并继续打印,直到所有矩阵值都显示出来。下面是我的代码片段。提前谢谢

我的计算矩阵按钮的代码

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)        {                                         
   match_reward = Integer.parseInt (match_tf.getText());
   mismatch_penalty =Integer.parseInt (mismatch_tf.getText());
   gap_cost=Integer.parseInt(gap_tf.getText());
   build_matrix();
   collumn_tf.setText(String.valueOf(max_col));
   row_tf.setText(String.valueOf(max_row));
   highest_score_tf.setText(String.valueOf(max_score));
  StringBuilder sb = new StringBuilder();
  for (int i =0; i < rows; i++) {
   for (int j = 0; j < cols; j++) {

          sb.append(String.valueOf(matrix[i][j]));
    sb.append(' ');
    }
}
 matrix_tf.setText(sb.toString());
}                         
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
match_reward=Integer.parseInt(match_tf.getText());
不匹配_惩罚=Integer.parseInt(不匹配_tf.getText());
gap_cost=Integer.parseInt(gap_tf.getText());
构建_矩阵();
collumn_tf.setText(String.valueOf(max_col));
row_tf.setText(String.valueOf(max_row));
最高分数\u tf.setText(String.valueOf(max_分数));
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
构建我的矩阵的代码

     private void build_matrix() {
    String seq1 = sequence1_tf.getText();
    String seq2 = sequence2_tf.getText();



    int r, c, ins, sub, del;

    rows = seq1.length();
    cols = seq2.length();

    matrix = new int [rows][cols];

    // initiate first row
    for (c = 0; c < cols; c++)
        matrix[0][c] = 0;

    // keep track of the maximum score
    max_row = max_col = max_score = 0;

    // calculates the similarity matrix (row-wise)
    for (r = 1; r < rows; r++)
    {
        // initiate first column
        matrix[r][0] = 0;

        for (c = 1; c < cols; c++)
        {
                        sub = matrix[r-1][c-1] +     scoreSubstitution(seq1.charAt(r),seq2.charAt(c));
                        ins = matrix[r][c-1] + scoreInsertion(seq2.charAt(c));
                        del = matrix[r-1][c] + scoreDeletion(seq1.charAt(r));

            // choose the greatest
            matrix[r][c] = max (ins, sub, del, 0);

            if (matrix[r][c] > max_score)
            {
                // keep track of the maximum score
                max_score = matrix[r][c];
                max_row = r; max_col = c;

            }


        }
    }

}
private void build_matrix(){
String seq1=sequence1_tf.getText();
String seq2=sequence2_tf.getText();
int r、c、ins、sub、del;
行=seq1.length();
cols=seq2.长度();
矩阵=新整数[行][列];
//启动第一行
对于(c=0;c最大分数)
{
//记录最高分
最大分数=矩阵[r][c];
最大行=r;最大列=c;
}
}
}
}

只需在内部循环后附加一个
\n
换行符:

for (int i =0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        sb.append(String.valueOf(matrix[i][j]));
        sb.append(' ');
    }
    sb.append('\n');
}
for(int i=0;i
使用
System.out.println(“\n”)
行矩阵之后,您是否尝试了什么,还是只是等待我们完成您的家庭作业?