gpt4 book ai didi

java - 如何在 Eclipse 中快速测试 Java 方法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:07:05 24 4
gpt4 key购买 nike

我的 Java 代码中有一个简单的方法,例如

private int specialAdd (int a, int b) {
if(a < 100) {
return 100 * a + b;
} else {
return a + b;
}
}

有没有一种方法可以在 Eclipse 中使用给定的参数值“运行/调试选定的代码”?我真的很想只运行这个方法并查看 ab 的几个 a 值的结果。

最佳答案

Is there a way to "run/debug selected code" with given parameter values in eclipse?

不,Eclipse 需要一个常规的 main 方法或一个 @Test 方法作为入口点。

当我想做一个“一次性”迷你测试时,我通常做的是

class YourClass {
private int specialAdd (int a, int b) {
if(a < 100) {
return 100 * a + b;
} else {
return a + b;
}

public static void main(String[] args) {
System.out.println(new YourClass().specialAdd(10, 100));
}
}

然后运行(或调试)当前类。 (专业提示:键入 main 并按 Ctrl+Space 将其展开为完整的 main 方法)


从 JDK 9 开始,您还可以使用 jshell 进行此类测试:

$ ./jshell
| Welcome to JShell -- Version 1.9.0-internal
| Type /help for help

-> int specialAdd(int a, int b) {
>> if (a < 100) {
>> return 100 * a + b;
>> } else {
>> return a + b;
>> }
>> }
| Added method specialAdd(int,int)

-> specialAdd(10, 5);
| Expression value is: 1005
| assigned to temporary variable $2 of type int

关于java - 如何在 Eclipse 中快速测试 Java 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com