英文:
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<TestDive> 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 < 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);
}
}
}
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 < 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) {
        // 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 < diffScores.length; j++) {
            // print each element of the array separated by a tab `\t` and formatted to one decimal point
            System.out.printf("%7.1f\t%7.1f\t%7.1f", diffScores[j][0], diffScores[j][1], diffScores[j][2]);
            // add a newline for next iteration
            System.out.println();
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论