我是基础知识编程的新手,我已经习惯了Java.
我想编写一个代码来计算第n次幂的数字而不使用循环.我一直在尝试使用大约4天前我所了解的"commons lang"中的重复方法.我在这个网站和其他网站上找到了很多信息,帮助我了解如何使用这个包装.
到目前为止,我下载了commons-lang3-3.1,然后将文件夹保存在与我的项目相同的文件夹中,并将jar文件添加到我的项目库中:
右键单击库
1然后添加JAR /文件夹
2然后我打开了commons-lang3-3.1文件夹
3并从多个4个选项中选择了"commons-lang3-3.1.jar":
这是一个代码,用于测试我从其他问题之一: -
0. package refreshingmemory;
1. import org.apache.commons.lang.StringUtils;
2. public class RefreshingMemory {
3.
4. public static void main(String[] args) {
5. String str = "abc";
6. String repeated = StringUtils.repeat(str, 3);
7. repeated.equals("abcabcabc");
8.
9. }
10. }
Run Code Online (Sandbox Code Playgroud)
第1行说包org.apache.commons.lang不存在.
第7行说应检查方法返回值
,如果我删除第1行,我在第6行找不到符号
如何成功导入?
Netbeans的屏幕截图:

我一直在尝试用Java编写一个简单的函数,它可以在不使用循环的情况下计算第n个幂的数字.
然后我发现Math.pow(a,b)类......或者方法仍然无法区分这两个与理论不太相关的东西.所以我写了这个..
public static void main(String[] args) {
int a = 2;
int b = 31;
System.out.println(Math.pow(a, b));
}
Run Code Online (Sandbox Code Playgroud)
然后我想制作我自己的Math.pow而不使用循环我希望它看起来比循环更简单,比如使用某种类型的重复我做了很多研究直到我遇到了commons-lang3包我试过使用StringUtils.repeat
到目前为止,我认为这是语法: -
public static String repeat(String str, int repeat)
StringUtils.repeat("ab", 2);
Run Code Online (Sandbox Code Playgroud)
我过去24小时或更长时间遇到的问题是StringUtils.repeat(String str,int 2); 重复字符串而不是输出或数字或计算.
我能做些什么来克服这个问题,还是有其他更好的方法来创建计算能力的函数?不使用循环或Math.pow
这可能很有趣,但我花了很多时间才发现StringUtils.repeat只重复字符串,这就是我试图克服它的方式.这有帮助
public static int repeat(int cal, int repeat){
cal = 2+2;
int result = StringUtils.repeat(cal,2);
return result;
}
Run Code Online (Sandbox Code Playgroud)
我可以不使用递归可能是这样的事情
public static RepeatThis(String a)
{
System.out.println(a);
RepeatThis(a);
} …Run Code Online (Sandbox Code Playgroud)