如何在代码中返回int? [英] How do I return int in my code ?

查看:100
本文介绍了如何在代码中返回int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台的末尾有不需要的号码200。



我尝试了什么:



I have unwanted number "200" at the end in console.

What I have tried:

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;
    }
}

推荐答案

这两行有助于导致不需要的200:

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

return result;



乘法表的整个打印是因为 multiplicationTable 中的 println 而发生的。最后一个是10 * 20或200,然后将其存储在结果中,然后返回。并且您编写了打印此返回值的指令。



为了避免在结尾处使用200,请使用 multiplicationTable a void ,删除结果变量(如果你不想返回任何内容,则整个变量变为无用),删除 return 语句并替换 System.out.println(m.multiplicationTable(10,20)); 使用 m.multiplicationTable(10,20);


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);.


引用:

我在控制台的末尾有不需要的数字200。

I have unwanted number "200" at the end in console.



你忘了告诉我们想要什么,所以我们只能猜测。 />
按照设计,您可以在表格后打印乘法表的最后结果。

这段代码非常简单,可以成为调试器学习的理想选择。

学习调试器有两个理由:

- 你自己学习如何找到bug。

- 你提高了对代码,因此你的学习曲线。

-----

你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码是什么做和你的任务是比较它应该做什么。

调试器中没有魔法,它不知道你应该做什么,它没有发现bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

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

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


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.


这篇关于如何在代码中返回int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆