【问题标题】:Power table using while loop formatting and logic error使用while循环格式和逻辑错误的功率表
【发布时间】:2019-04-25 04:07:37
【问题描述】:

我正在尝试制作一个从 1 到 5 的表格,其中最多显示 6 个值。

例如,第 2 列将从 1,2,4,8,16,32,64 开始并停在那里。

我无法获得正确的表格格式。由于数字与它们应该在的位置不一致。

例如:

我现在面临的问题是这样的

1 2 3 4 5
1 1 1 1 1 1 1 2 4 8 16 等等等等

将不胜感激,我的代码在下面。

 int powNumb=5;
 int powValue=6;

for (int i = 1; i <= powValue; i++) {
            System.out.printf("%10d",i);
        }
        System.out.println();
        for (int i = 1; i <= powNumb; i++) {
            for (int j = 0; j <=powValue; j++) {
                System.out.printf("%10.0f",Math.pow(i, j));
            }
        }

【问题讨论】:

    标签: java string algorithm for-loop math


    【解决方案1】:

    这应该对你有帮助

    for (int i = 1; i <= powNumb; i++) {
        System.out.printf("%10d", i); //Print the number (1st col)
        for (int j = 0; j <= powValue; j++) { //This loop prints the powers of the curent number 'i'
            System.out.printf("%10.0f", Math.pow(i, j));
        }
        System.out.println(); //To end the current row
    }
    

    打印出来

    num   num^0  num^1  num^2 ... num^powValue
    

    其中 num 是从 1 到 powNumb

    输出

     1         1         1         1         1         1         1         1
     2         1         2         4         8        16        32        64
     3         1         3         9        27        81       243       729
     4         1         4        16        64       256      1024      4096
     5         1         5        25       125       625      3125     15625
    

    【讨论】:

      【解决方案2】:

      您的意思是每个元素都使用相同的基数,因此不需要内部循环:

      for (int i = 1; i <= powNumb; i++) {
          System.out.printf("%10.0f", Math.pow(powValue, i));
      }
      

      这样,权力的基础总是powValue

      【讨论】:

        【解决方案3】:

        首先,您需要在内部 for 循环中的某处使用 println 语句来分隔行。

        其次,您需要将通话中的ij切换为Math.pow。因为按照当前的设置方式,每一行的值都是 i = row number 的 0 到 6 的幂。例如,第一行是 1^0 1^1 1^2 1^3 1^4 1^5 1^6。然后,第二行将是2^0 2^1 2^2 2^3 2^4 2^5 2^6 但是,您希望第一行是1^0 2^0 3^0 4^0 5^0,第二行是1^1 2^1 3^1 4^1 5^1,等等。所以您的代码应该改为这样,

        int powNumb=5;
        int powValue=6;
        
        for (int i = 1; i <= powNumb; i++) {
            System.out.printf("%10d",i);
        }
        for (int i = 0; i <= powValue; i++) {
            System.out.println();
            for (int j = 1; j <=powNumb; j++) {
                System.out.printf("%10.0f",Math.pow(j, i));
            }
        }
        

        输出:

         1         2         3         4         5
         1         1         1         1         1
         1         2         3         4         5
         1         4         9        16        25
         1         8        27        64       125
         1        16        81       256       625
         1        32       243      1024      3125
         1        64       729      4096     15625
        

        另外,我必须在 for 循环条件中切换 powNumbpowValue

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多