Java表格问题:获取列的平均值

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

Java table issue with getting average of colums

问题

for (int i = 1; i <= rows; i++) {   // 表格
    for (int j = 1; j <= cols; j++) { // 表格
        int result = random.nextInt(b - a) + a; // 生成随机数字
        System.out.print(result + " ");
        average_rows += result;
    }
    if (i <= rows) {
        average_rows = average_rows / 4;
        System.out.print(String.format("%.4f", average_rows) + "\n");
    }
}

(注意:我已经翻译了指定的代码部分,如果还有其他需要翻译的内容,请提供更多信息。)

英文:

so I just started with java and my task is to make simple table filled with random numbers. Have to get each average value of rows and columns at the end of line. I did almost everything tryed to play with arrays, been calling variables out of specific loop but nothing worked, so looking for any ideas or any help i could get.

package com.company;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Main {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.print(&quot;Enter your name: &quot;);
    String name = sc.next();
    System.out.print(&quot;Enter your surname: &quot;);
    String surname = sc.next();;
    int name_length = name.length();
    int surname_length = surname.length();
    int a = 0; // Balses(Vowels)
    name = name.toLowerCase();

    for(int i = 0; i &lt; name.length(); ++i){
        char ch = name.charAt(i);
        if(ch == &#39;a&#39; || ch == &#39;e&#39; || ch == &#39;i&#39;
                || ch == &#39;o&#39; || ch == &#39;u&#39;){
            ++a; // Vowels
        }
    }
    int b = name.length() + surname.length();
    int rows = 4;
    int cols = 4;
    double average_rows=0;
    int average_cols = 0;
    Random random = new Random();

    for (int i = 1; i &lt;= rows; i++)
    {   // Table
        for (int j = 1; j &lt;= cols; j++) { // Table
            int result = random.nextInt(b - a) + a; // Making random numbers
            System.out.print(result+&quot; &quot;);
            average_rows += result;
            }
        if (i &lt;= rows)
            {
            average_rows = average_rows/4;
            System.out.print(String.format(&quot;%.4f&quot;,average_rows) + &quot;\n&quot;);
            }
        }

        System.out.println(&quot;\nName length: &quot; + name_length + &quot; Surname length: &quot; + surname_length);
        System.out.println(&quot;Vowels: &quot; + a + &quot; Letters of name and surname: &quot; + b);
    }
}

Specific place:

for (int i = 1; i &lt;= rows; i++) {   // Table
            for (int j = 1; j &lt;= cols; j++) { // Table
                int result = random.nextInt(b - a) + a; // Making random numbers
                System.out.print(result+&quot; &quot;);
                average_rows += result;
                }
            if (i &lt;= rows)
                {
                average_rows = average_rows/4;
                System.out.print(String.format(&quot;%.4f&quot;,average_rows) + &quot;\n&quot;);
                }
            }

Nailed it with rows but talking about columns feeling in dead end.

答案1

得分: 0

如果您需要按列计算每个单元格的平均值,可以创建另外两个for循环,然后只需交换行和列。

如果您只需要按列计算行的平均值,

for (int i = 1; i <= rows; i++){
   for (int j = 1; j <= cols; j++) { 
      // 你的代码
   }
   average_rows = average_rows/4;
   System.out.print(String.format("%.4f",average_rows) + "\n");

   // 列部分的代码添加你需要的内容到变量totalColumn
   totalColumn += result;
}
average_cols = totalColumn /4;

此外,在Java中,最好使用驼峰命名法而不是下划线。

我没有看到表格可视化;看起来是使用随机数来获取2个平均值的2个for循环。但是如果您需要存储每个单元格,我建议使用2D数组。

另外

if (i <= rows)

是不必要的,因为它会始终运行,因为它在

for (int i = 1; i <= rows; i++)

内部。

英文:

If you need the average of each cell by col, you can create another 2 for loops and just switch the rows and cols.

If you just need the average of rows of col,

for (int i = 1; i &lt;= rows; i++){
   for (int j = 1; j &lt;= cols; j++) { 
      // your code
   }
   average_rows = average_rows/4;
   System.out.print(String.format(&quot;%.4f&quot;,average_rows) + &quot;\n&quot;);


   // column part of code add whatever you need to the variable totalColumn
   totalColumn += result;
}
average_cols = totalColumn /4;

Also in java, it's better practice to use camelCase than _

I'm not seeing the table visualization; looks like 2 for loops to get 2 averages using random. But if you need to store each cell, I would use a 2d array.

Also

if (i &lt;= rows)

Is not necessary because it will always run, since it's inside

for (int i = 1; i &lt;= rows; i++)

huangapple
  • 本文由 发表于 2020年9月15日 06:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892487.html
匿名

发表评论

匿名网友

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

确定