您好,欢迎访问一九零五行业门户网

如何让ChatGPT充当细致入微的Java代码优化工?

注:本文使用new bing(gpt4.0)演示
1、让他扮演一个java软件开发者第一步:我们让chatgpt扮演一个java软件开发者的角色提示词插件:地址:chatgpt bingchat gpt3 prompt generator app (streamlit) - a hugging face space by kaludijava software developer  java软件开发者
i want you to act as a java software developer. i will provide you with a list of commands and you will implement them. my first request is i need help creating a java application.
2、准备一个不太好的java代码示例第二步:我们准备一个写得不太好的java代码示例bad java example:public int calculateusingifelse(int a, int b, string operator) { int result = 0; if (operator.equals(add)) { result = a + b; } else if (operator.equals(subtract)) { result = a - b; } else if (operator.equals(multiply)) { result = a * b; } else if (operator.equals(divide)) { if (b == 0) { throw new illegalargumentexception(cannot divide by zero); } result = a / b; } else if (operator.equals(modulus)) { if (b == 0) { throw new illegalargumentexception(cannot divide by zero); } result = a % b; } else if (operator.equals(power)) { result = (int) math.pow(a, b); } else if (operator.equals(square root)) { if (a a + b), subtract((a, b) -> a - b), multiply((a, b) -> a * b), divide((a, b) -> { if (b == 0) { throw new arithmeticexception(cannot divide by zero); } return a / b; }), modulus((a, b) -> { if (b == 0) { throw new arithmeticexception(cannot divide by zero); } return a % b; }), power((a, b) -> (int) math.pow(a, b)), square_root((a, b) -> { if (a -a); private final operationstrategy strategy; operator(operationstrategy strategy) { this.strategy = strategy; } public int apply(int a, int b) { return strategy.apply(a, b); } } // 定义一个方法,根据运算符和操作数来计算结果 public int calculateusingstrategy(int a, int b, string operatorname) { // 根据运算符名称找到对应的枚举常量,如果没有找到则抛出异常 operator operator = operator.valueof(operatorname.touppercase()); if (operator == null) { throw new illegalargumentexception(invalid operator); } // 调用枚举常量的apply方法来执行相应的策略 return operator.apply(a,b); }
4、让他写个单测第四步:我们最后让他收下尾——写个单测吧
import org.junit.test; import static org.junit.assert.*; // 定义一个测试类,用@test注解标记要执行的测试方法 public class calculatortest { // 定义一个测试方法,用assertequals方法来验证计算结果是否正确 @test public void testadd() { int result = calculateusingstrategy(3, 5, add); assertequals(8, result); } // 定义其他的测试方法,分别测试不同的运算符和操作数 @test public void testsubtract() { int result = calculateusingstrategy(10, 4, subtract); assertequals(6, result); } @test public void testmultiply() { int result = calculateusingstrategy(7, 9, multiply); assertequals(63, result); } @test public void testdivide() { int result = calculateusingstrategy(12, 3, divide); assertequals(4, result); } // 定义一个测试方法,用assertthrows方法来验证抛出了预期的异常 @test public void testdividebyzero() { assertthrows(arithmeticexception.class, () -> calculateusingstrategy(10,0,divide)); } // 定义其他的测试方法,分别测试不同的异常情况 @test public void testmodulusbyzero() { assertthrows(arithmeticexception.class, () -> calculateusingstrategy(10,0,modulus)); } @test public void testsquarerootofnegativenumber() { assertthrows(arithmeticexception.class, () -> calculateusingstrategy(-4,-2,square root)); } // 定义一个测试方法,用assertthrows方法来验证抛出了预期的异常 @test public void testinvalidoperator() { assertthrows(illegalargumentexception.class, () -> calculateusingstrategy(10,-2,foo)); } }
最后,数据安全是最大的问题,不要乱贴数据到 chatgpt,尤其是涉及后端核心存储账户密码、公司核心业务数据、部门核心战略规划等。因为首先,chatgpt会使用你的问答语料进行训练;其次,你无法预料在什么prompt提示词下,llm模型会讲你无意中泄露的信息回答出去。
瑕不掩瑜,chatgpt为代表的llm模型,在充当我们无所不知的老师、充当不知疲倦的通用util代码编写者这些角色时能极大的提高我们的开发效率,尤其在数据分析、前端、单测、重构等领域。
就像文章第一步写的一样,chatgpt就像是一个百变身份,你可以让他扮演任何角色,而每一个角色都能在这个角色范围内帮助我们获得更美好的生活。
更有意思的用法期待大家的发掘。
以上就是如何让chatgpt充当细致入微的java代码优化工?的详细内容。
其它类似信息

推荐信息