Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Right now my code calls the method but it only calls a specific argument (parameter) that I myself put in. I would instead want it to call all the arguments listed below in just 1 System.out.print();

The code below is both how the program is meant to run and how it looks at the moment.

Any type of help is appreciated!

Just switching the variable "a" with f1 * f2 is not what I want in this code, even though it would be super easy.

What I have tried:

public class ExampleProgram {

	public static int multiply(int factorOne, int factorTwo) {
		return factorOne * factorTwo;
	}

	public static void main(String[] args) {
		for (int f1 = 1; f1 < 5; f1++) {
			for (int f2 = 1; f2 < 5; f2++) {
			     int a = multiply(1, 1);

				System.out.println(f1 + "*" + f2 + " är " + a);
			}
		}
	}

}

Expected
1*1 är 1
1*2 är 2
1*3 är 3
1*4 är 4
2*1 är 2
2*2 är 4
2*3 är 6
2*4 är 8
3*1 är 3
3*2 är 6
3*3 är 9
3*4 är 12
4*1 är 4
4*2 är 8
4*3 är 12
4*4 är 16

Got 
1*1 är 1
1*2 är 1
1*3 är 1
1*4 är 1
2*1 är 1
2*2 är 1
2*3 är 1
2*4 är 1
3*1 är 1
3*2 är 1
3*3 är 1
3*4 är 1
4*1 är 1
4*2 är 1
4*3 är 1
4*4 är 1
Posted
Updated 16-Jun-21 15:28pm
v2

1 solution

try to replace
Java
int a = multiply(1, 1);

with
Java
int a = multiply(f1, f2);


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

jdb - The Java Debugger[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900