The Paulsm4 tip is dead - you don't want to do everything in two functions. You must have an input array, user input, multiplication and output, each in its own functions. You may find other ways to split your program as you work on it. I highly recommend breaking your program down into functions as much as possible, especially because you can debug small chunks much more efficiently than large pieces. (What's wrong with your current program? Input? Calculation? Data warehouse sizes, as Adam noted? It's hard to say because you donβt know which functions work fine and which still don't work.)
I would like to suggest the following change: do not put the else your tests so far if you can help. Think about it:
int n = getIt.nextInt(); if (n > 0){ System.out.println("Input 1 integer p: "); int p = getIt.nextInt(); if(p > 0){ } else{ System.out.println("Woops, you entered a negative p."); } } else{ System.out.println("Woops, you entered a negative n."); }
:
int n = getIt.nextInt(); if (n <= 0) { System.out.println("Woops, you entered a negative n."); } System.out.println("Input 1 integer p: "); int p = getIt.nextInt(); if (p <= 0) { System.out.println("Woops, you entered a negative p."); }
Writing code in this way will simplify maintenance and will probably also make it easier to find units that can be deleted in their own routines. (Consider a function that takes a String prompt and Scanner in and returns a number greater than zero - it can query and repeat the request as necessary, and the result will be exactly what the basic code requirements are as clean as possible. This function is more obvious, useful when code is rewritten this way.)
As for the error that brought you here, I think this is probably the culprit:
System.out.printf("%f ", Math.pow(matrixMult(M[i], M[i])[j][k], q));
Here you calculate a lot, but donβt save the results at all. You do not need these results for further calculation?
source share