未获得矩阵JAVA的主对角线的必需输出

时间:2017-04-16 23:56:53

标签: java for-loop matrix

该程序假设: "向用户询问数字n。创建一个nxn整数矩阵。使用5的种子用0到9之间的随机数填充矩阵(记住,当我们为随机数生成器播种时,我们得到可预测的"随机数"数字)。

对于完全信用,计算从矩阵左上角开始的对角线的总和(index [0] [0])。将该总和显示在屏幕上。"

教授回答: "对于100 x 100矩阵,主对角线的总和应为503,另一对角线的总和应为444。"

我的程序回答:主对角线为700。所以我猜测我的MakeandFillMatrix方法有问题。

import java.util.*;
/*

*/
public class TesterProject
{
    public static void main(String [] args)
    {
        int n = getMatrixSize();
        int[][] m = makeAndFillMatrix(n);
        printMatrix(m);

        int sumD1 = calculateMainDiagonal(m);
        System.out.println("The sum of the main diagonal is " + sumD1);
    }
    public static int getMatrixSize()
    {
        Scanner S = new Scanner(System.in);

        System.out.println("give me a int to create the matrix");
        while(!S.hasNextInt())
        {
            System.out.println("I need an integer!");
            S.next();
        }
        int n = S.nextInt();
        return n;
    }
    public static int [][] makeAndFillMatrix(int n)
    {
        Random generator = new Random(5);
        int [][] r = new int[n][n];
        int rand = generator.nextInt(10);

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                r[i][j] = rand;
                //rand++;
            }
        }
        return r;
        /*
        for(int i = 0; i < r.length; i++)
        {
            for(int j = 0; j < r[i].length; j++)
            {
                r[i][j]= rand;
            }
        }
        return r;
        */
    }
    public static void printMatrix(int [][] matrix)
    {
        for(int r = 0; r < matrix.length; r++)
        {
            for(int c = 0; c < matrix[r].length; c++)
            {
                System.out.print(matrix[r][c] + " ");
            }
            System.out.println();
        }
    }
    public static int calculateMainDiagonal(int [][] m)
    {
        int total = 0;
        for (int r = 0; r < m.length; r++)
        {
                total += m[r][r];
        }
        return total;
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是您只生成一次随机数。你会看到你的矩阵只有7个,无论如何。当你写foreach($questions as $question) { ?> <h3><?php echo $question['question'] ?></h3> <div class = "col-xs-12"> <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option1']?>" /> <label for="question-1-answers-A">A)<?php echo $question['option1'] ?> </label> </div> <div class = "col-xs-12"> <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option2']?>" /> <label for="question-1-answers-B">B)<?php echo $question['option2'] ?></label> </div> <div class = "col-xs-12"> <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option3']?>" /> <label for="question-1-answers-C">C)<?php echo $question['option3'] ?></label> </div> <div class = "col-xs-12"> <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option4']?>" /> <label for="question-1-answers-D">D)<?php echo $question['option4'] ?></label> <br> <br> </div> <?php } echo "<input type='hidden' name='subaction' id='subaction' value='chkQuestion'> <button class='btn btn-primary' name='submit' type='submit'>Submit</button> </form>"; } else echo "No Quiz Found </form>"; ?> </div> 时,你真的应该将该元素设置为一个新的随机数。删除行r[i][j] = rand并将前面提到的行更改为rand = generator.nextInt(10)