long d1 = System.currentTimeMillis(); System.out.println("%d阶乘的结果:%s%n", 10, factorial(10)); long d2 = System.currentTimeMillis(); System.out.println("递归费时:%s%n", d2 - d1); } static long factorial(int n){ if(n == 1){ return 1; }else return n * factorial(n-1); }为什么运行不了
时间: 2023-12-19 18:06:31 浏览: 37
这段代码中的`System.out.println()`方法的格式字符串使用了C语言风格的格式化方式,应该使用Java的格式化方式。即用`%d`代替`%s`,代码应该修改为:
```
long d1 = System.currentTimeMillis();
System.out.printf("%d阶乘的结果:%d%n", 10, factorial(10));
long d2 = System.currentTimeMillis();
System.out.printf("递归费时:%d%n", d2 - d1);
static long factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
```
这样就能正常运行了。
相关问题
public static void main(String[] args) { long startTime = System.currentTimeMillis(); int a = 10; int result = 1; while (a>1){ result *=a*(a-1); a-=2; } long endTime = System.currentTimeMillis(); System.out.println(result); System.out.printf("优化后的循环费时: %d ns%n", endTime - startTime); }优化
这段代码的问题在于它没有正确地计算阶乘,应该是 `result *= a;` 而不是 `result *= a*(a-1);`。另外,对于优化循环费时,可以考虑使用更高效的算法,如递归或者使用数学公式计算阶乘。以下是一个使用递归计算阶乘的示例代码:
```
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int n = 10;
int result = factorial(n);
long endTime = System.currentTimeMillis();
System.out.println(result);
System.out.printf("优化后的循环费时: %d ns%n", endTime - startTime);
}
private static int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n-1);
}
```
这个代码使用递归计算阶乘,比起原来的循环计算更加简洁和高效。
多线程中 long end=System.currentTimeMillis();在哪里使用
在多线程中,`long end=System.currentTimeMillis()`通常用于计算线程执行时间。具体来说,可以在线程的`run()`方法中使用`System.currentTimeMillis()`记录线程的开始时间,并在线程执行完毕后再次调用`System.currentTimeMillis()`记录结束时间,然后计算两者之间的差值,即可得到线程的执行时间。
例如:
```java
public class MyThread extends Thread {
@Override
public void run() {
long start = System.currentTimeMillis();
// 执行线程任务
// ...
long end = System.currentTimeMillis();
System.out.println("Thread execution time: " + (end - start) + "ms");
}
}
```
在上述代码中,`System.currentTimeMillis()`方法被用于记录线程的开始时间和结束时间,并计算线程的执行时间。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)