2021年2月10日星期三

Update powered integers so its in one line

The logic of my code works but the problem I'm having is in the System.out.println(); line.

Example of what my code does:

This will generate integers from the number you put in the input parameter(n) in order and put them to the power of what you put in the input parameter(n).

This means below the input parameter n is equal to 3.

totalCue(3) = 1^3 + 2^3 + 3^3 which will obviously equal 36

I want my ouput to be this 1 + 8 + 27 = 36 but instead it is this

1 +  = 1  8 +  = 9  27 +  = 36  

What do I do this fix this or update all the numbers in only 1 line. BTW: I also tried doing just System.out.print(); but it didn't work.

public class CUETOTAL  {      public int totalCue(int n)      {          int result = 0;                              for (int x = 1; x <= n; x++){              result += (int)Math.pow(x, n);              System.out.print((int)Math.pow(x, n)+" + " + " = " +result);          }          return result;                          }            public static void main(String args[]){                    CUETOTAL n = new CUETOTAL();                    n.totalCue(3);                          }  }  
https://stackoverflow.com/questions/66147807/update-powered-integers-so-its-in-one-line February 11, 2021 at 10:06AM

没有评论:

发表评论