有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何打印一组数字以及从1到列表中每个数字的平方和?

我如何用两种不同的方法编写这个问题?我知道这可以在一个循环和一个嵌套循环中完成,但它需要这样做。我知道这都错了,但我需要帮助理解逻辑

    public static int numloop(int n){
        int nd = 0;
        for(n = 5; n <= 49; n += 2){
            nd = ndiv(n);
        }
      return nd;
    }
    public static int ndiv(int numb){    
        int sumsq = 0; 
        for( int x = 1; x <= numb; x++ ){
            sumsq += x * x;

        }  
     return sumsq;   
    }
     public static void main(String[]args){

       System.out.println("NUMBER\t" + "SUMSQ");
       System.out.println(n + nd);
    }

输出应该如下所示:

数| |平方和

5 | | 55

7 | | 140

9 | | 285


共 (2) 个答案

  1. # 1 楼答案

    Tony-无法准确地计算出您想要的代码,所以我冒昧地让它更灵活一些。你应该很容易限制它只做你想做的事

    public class SumSquare {
    
        public static int numloop(int start, int end, int increment){
            int nd = 0;
            for(int i = start; i <= end; i += increment){
                nd = ndiv(i);
                System.out.println(i + " || " + nd);
            }
          return nd;
        }
        public static int ndiv(int numb){    
            int sumsq = 0; 
            for( int x = 1; x <= numb; x++ ){
                sumsq += x * x;
    
            }  
         return sumsq;   
        }
         public static void main(String[]args){
           System.out.println("Number" + " || " + "Sum of square");
           numloop(5, 49, 2);
        }    
    }
    

    输出:

    run:
    Number || Sum of square
    5 || 55
    7 || 140
    9 || 285
    11 || 506
    13 || 819
    15 || 1240
    17 || 1785
    19 || 2470
    21 || 3311
    23 || 4324
    25 || 5525
    27 || 6930
    29 || 8555
    31 || 10416
    33 || 12529
    35 || 14910
    37 || 17575
    39 || 20540
    41 || 23821
    43 || 27434
    45 || 31395
    47 || 35720
    49 || 40425
    BUILD SUCCESSFUL (total time: 0 seconds)
    
  2. # 2 楼答案

    这很简单@Tony。如果您希望使用两种方法完成此操作,则只需按如下方式将其拆分:

    • 一种以输入数为参数进行计算的方法 平方和(在另一种方法中找到的平方)
    • 另一种计算数字平方的方法

    以下是该项目流程的概述:

    1. 接受这个号码
    2. 将数字输入上述第1种方法
    3. 在第一种方法中,从1循环到数字。。为每个i值调用Square()方法。同时计算总和
    4. 所有迭代完成后打印总和

    我已将代码及其输出附在下面:

    代码:

    import java.io.*;
    
    class Main {
    
      public static void main(String[] args)throws IOException {
        InputStreamReader ip = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ip);
        int c = 1;
        do {
          System.out.println("Enter a number: ");
          int n = Integer.parseInt(br.readLine());
          squaresum(n);
          System.out.println("\nPress 1 to continue, 0 to stop..");
          c = Integer.parseInt(br.readLine());
        }while(c == 1);
        System.out.println("Stopped.");
      }
    
      public static void squaresum(int num) {
        int n = 0;
        for(int i = 1; i <= num; i++)
          n += square(i);
        System.out.println("Sum is: " + n);
      }
    
      public static int square(int n) {
        return (n*n);
      }
    
    }
    

    输出:

    Enter a number:

    5

    Sum is: 55

    Press 1 to continue, 0 to stop..

    1

    Enter a number:

    9

    Sum is: 285

    Press 1 to continue, 0 to stop..

    0

    Stopped.