gpt4 book ai didi

java - 平方和计算

转载 作者:行者123 更新时间:2023-12-01 04:49:46 25 4
gpt4 key购买 nike

Write a program that displays the sum of the squares of each integer in the multiplication table. integers 1-10
From Professor:The values in the table should be the rowNum squared plus the colNum squared. So, the first entry is 2 (rowNum = 1, colNum = 1. 1^2 + 1^2 = 1 + 1 = 2). The next entry is 5 (rowNum = 1, colNum = 2. 1^2 + 2^2 = 1 + 4 = 5).

我的代码除了标题之外不会打印任何内容。无需计算。表格应该像这样:

    1  2  3   4   5 ....1   2  5  10  17  26....2   5  8  13  20  29.... 
public class Mobilesumcp3ex3 {

/**
* @param args the command line arguments
*/

public static void main ( String [] args )
{
// Display a title
System.out.println( " Sum of Multipication Table" );
System.out.println( "-------------------" );


int num[] = new int[10];
int sq[] = new int[10];
int sum = 0;
for (int i = 0; i < num.length; i++) {

sq[i] = num[i] * num[i];
sum += sq[i];
}
System.out.println(num+"\t"+sum);
System.out.println("Sum of the square of numbers: " + sum);
}
}

最佳答案

数组 num[] 未初始化。因此, sq[i] = num[i] * num[i]; 有问题,因为 num[i] 不存在(那里没有值)。除此之外,代码看起来不错。

关于java - 平方和计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15191482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com