java - 平方和计算

标签 java

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/

相关文章:

java - 从servlet读取POST请求参数

java - 良好实践 : Exception Handling

java - 如何在 Spring Boot 应用程序中将参数传递给自定义 JSON 反序列化器

Java Runtime.exec 转义字符串中的参数

Java SWT 创建监听器来更改标签文本并返回数据

java - 可以代理某些类但不能代理其他类

Java - 以全双工方式写入对套接字输出流阻塞的调用

Java部门并没有按照我的预期进行。

java - Visual Studio代码: terminal input accents (java. util.Scanner)

java - Android 项目无法运行。显示错误