英文:
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("Enter your name: ");
String name = sc.next();
System.out.print("Enter your surname: ");
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 < name.length(); ++i){
char ch = name.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u'){
++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 <= rows; i++)
{ // Table
for (int j = 1; j <= cols; j++) { // Table
int result = random.nextInt(b - a) + a; // Making random numbers
System.out.print(result+" ");
average_rows += result;
}
if (i <= rows)
{
average_rows = average_rows/4;
System.out.print(String.format("%.4f",average_rows) + "\n");
}
}
System.out.println("\nName length: " + name_length + " Surname length: " + surname_length);
System.out.println("Vowels: " + a + " Letters of name and surname: " + b);
}
}
Specific place:
for (int i = 1; i <= rows; i++) { // Table
for (int j = 1; j <= cols; j++) { // Table
int result = random.nextInt(b - a) + a; // Making random numbers
System.out.print(result+" ");
average_rows += result;
}
if (i <= rows)
{
average_rows = average_rows/4;
System.out.print(String.format("%.4f",average_rows) + "\n");
}
}
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 <= rows; i++){
for (int j = 1; j <= cols; j++) {
// your code
}
average_rows = average_rows/4;
System.out.print(String.format("%.4f",average_rows) + "\n");
// 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 <= rows)
Is not necessary because it will always run, since it's inside
for (int i = 1; i <= rows; i++)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论