Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have unwanted number "200" at the end in console.

What I have tried:

Java
public class Main {

    public static void main(String[] args) {
        Main m = new Main();

        
        System.out.println(m.multiplicationTable(10, 20));

    }

    
    public int multiplicationTable(int number1, int number2) {
        int result = 0;
        for (int j = 11; j <= number2; j++) {
            for (int i = 1; i <= number1; i++) {

                System.out.println(i + " * " + j + " = " + i * j);
                result = i * j;

            }
        }

        return result;
    }
}
Posted
Updated 8-May-18 9:51am
v2

These two lines contribute to causing the unwanted "200":
Java
System.out.println(m.multiplicationTable(10, 20));
Java
return result;

The whole printing of the multiplication table happens because of the printlns inside multiplicationTable. The last one is 10 * 20, or 200, and you store that in result, which you then return. And you wrote the instruction to print this return value.

To avoid that "200" at the end, make multiplicationTable a void, drop the result variable (if you don't want to return anything, the whole variable becomes useless), drop the return statement and replace System.out.println(m.multiplicationTable(10, 20)); with m.multiplicationTable(10, 20);.
 
Share this answer
 
Comments
Member 13817762 8-May-18 15:23pm    
Thank you very much . Done!
Quote:
I have unwanted number "200" at the end in console.

You forgot to tell us what is wanted, so we can only guess.
By design you code print the last result of multiplication table after the table.
This code is simple enough to be a good candidate for debugger learning.
There are 2 reasons to learn debugger:
- You learn ways to find bugs by yourself.
- you improve your understanding of code and thus your learning curve.
-----
Your code do not behave the way you expect, and 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 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[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
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