如何在每个列中生成更多数值?

huangapple go评论51阅读模式
英文:

How do I generate more values in each column?

问题

以下是您的代码部分的中文翻译:

我需要帮助看看我做错了什么

我试图生成3列中的10个随机值但到目前为止我只能生成一个

private static void displayDifficultyScore(ArrayList<TestDive> alist, double[] dArray)
{

    // 创建一个Random类
    Random rand = new Random();
    double[] diffScores = new double[10];
    double[] diffArray = new double[3];


    // 打印下一行
    System.out.println();


    // 循环难度分数
    for (int j = 0; j < diffScores.length; j++) {
        diffScores[j] = 2.0 + rand.nextDouble() * 3.0;
        System.out.printf("%-7.1f%n", diffScores[j]);
    }


}

public static void main(String[] args)
{
    double[] diffScores = new double[1];
    ArrayList<TestDive> alist = new ArrayList<TestDive>();
    for (int j = 0; j < diffScores.length; j++) {
        displayDifficultyScore(alist, diffScores);
    }
}
}

当前它只以1列打印出来

2.9
4.4
4.9
3.7
4.3
3.1
4.2
4.5
4.2
2.9

我想要实现的输出是

2.9    3.0    2.2
4.4    3.4    4.7
4.9    2.5    3.0
3.7    4.7    3.3
4.3    4.4    3.8
3.1    3.0    4.9
4.2    2.1    2.9
4.5    5.0    2.2
4.2    4.8    3.6
2.9    2.6    3.1

请注意,上述中文翻译部分仅包括代码部分,不包括问题和答案。

英文:

I need help to see what I'm doing wrong.

I'm trying to generate 10 random values in 3 columns, but so far I can only generate one.

	private static void displayDifficultyScore(ArrayList&lt;TestDive&gt; alist, double[] dArray)
{
// Create a Random class
Random rand = new Random();
double[] diffScores = new double[10];
double[] diffArray = new double[3];
// Print next line
System.out.println();		
// Loop for Difficulty Scores
for (int j = 0; j &lt; diffScores.length; j++) {
diffScores[j] = 2.0 + rand.nextDouble() * 3.0;
System.out.printf(&quot;%-7.1f%n&quot;, diffScores[j]);
}
}
public static void main(String[] args)
{
double[] diffScores = new double[1];
ArrayList&lt;TestDive&gt; alist = new ArrayList&lt;TestDive&gt;();
for (int j = 0; j &lt; diffScores.length; j++) {
displayDifficultyScore(alist, diffScores);
}
}
}

Currently it's only printing out in 1 column:

2.9
4.4
4.9
3.7
4.3 
3.1 
4.2 
4.5 
4.2 
2.9 

Output that I want to achieve:

2.9    3.0    2.2
4.4    3.4    4.7
4.9    2.5    3.0
3.7    4.7    3.3
4.3    4.4    3.8
3.1    3.0    4.9
4.2    2.1    2.9
4.5    5.0    2.2
4.2    4.8    3.6
2.9    2.6    3.1

答案1

得分: 1

public class Hello {
    private static double[][] generateDifficultyScores(int matrixLength, int matrixWidth) {
        // 创建一个Random类
        Random rand = new Random();
        double[][] diffScores = new double[matrixLength][matrixWidth];

        // 循环生成难度分数
        for (int i = 0; i < matrixLength; i++) {
            for (int j = 0; j < matrixWidth; j++) {
                diffScores[i][j] = 2.0 + rand.nextDouble() * 3.0;
            }
        }

        return diffScores;
    }

    public static void main(String[] args) {
        // 生成随机的难度分数(将其存储在一个值的2D数组或矩阵中)
        double[][] diffScores = generateDifficultyScores(10, 3);

        // 逐行打印矩阵中的每个数组
        for (int j = 0; j < diffScores.length; j++) {
            // 打印数组的每个元素,用制表符`\t`分隔,并格式化为一位小数
            System.out.printf("%7.1f\t%7.1f\t%7.1f", diffScores[j][0], diffScores[j][1], diffScores[j][2]);

            // 为下一次迭代添加换行符
            System.out.println();
        }
    }
}
英文:

You can try generating a 2D array (or matrix) first and then printing it

Something like this

public class Hello {
    private static double[][] generateDifficultyScores(int matrixLength, int matrixWidth) {
        // Create a Random class
        Random     rand       = new Random();
        double[][] diffScores = new double[matrixLength][matrixWidth];

        // Loop for Difficulty Scores
        for (int i = 0; i &lt; matrixLength; i++) {
            for (int j = 0; j &lt; matrixWidth; j++) {
                diffScores[i][j] = 2.0 + rand.nextDouble() * 3.0;
            }
        }

        return diffScores;
    }

    public static void main(String[] args) {
        // generate random difficulty scores (stores it in a 2D array or matrix of values)
        double[][] diffScores = generateDifficultyScores(10, 3);

        // print out each array in matrix (line by line)
        for (int j = 0; j &lt; diffScores.length; j++) {
            // print each element of the array separated by a tab `\t` and formatted to one decimal point
            System.out.printf(&quot;%7.1f\t%7.1f\t%7.1f&quot;, diffScores[j][0], diffScores[j][1], diffScores[j][2]);

            // add a newline for next iteration
            System.out.println();
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 13:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定