这段代码什么意思public class RecursionTest01 { public static void main(String[] args) { int jc = jc(100); System.out.println(jc); } private static int jc(int i) { if (i==1){ return 1; }else { return i+jc(i-1); } } }
时间: 2023-05-29 12:07:12 浏览: 82
这段代码是一个递归求阶乘的程序。程序中定义了一个私有的静态方法jc(int i),用来递归计算i的阶乘。如果i等于1,则直接返回1;否则,调用jc(i-1)方法求i-1的阶乘,然后将i乘以i-1的阶乘作为返回值。在main方法中,调用jc(100)方法求100的阶乘,并将结果输出到控制台。
相关问题
public class E { public static void main(String args[]){ int m = 100; System.out.println(m+"100");//【代码】 } l
在代码中,需要使用加号来将字符串和变量连接起来。修改后的代码如下:
```
public class E {
public static void main(String args[]) {
int m = 100;
System.out.println(m + "100");
}
}
```
输出结果为:`100100`。
public class Person private String name=“ Person” int age=0; Public class Child extends Person public String grade; public static void main (String args) Person p = new Child(); System.out.println(p.name);
这段代码有几个语法错误,应该修改为以下内容:
```java
public class Person {
private String name = "Person";
private int age=0;
}
public class Child extends Person {
public String grade;
public static void main(String[] args) {
Person p = new Child();
System.out.println(p.name);
}
}
```
在这个修正后的代码中,我们定义了 `Person` 和 `Child` 两个类。`Child` 继承自 `Person`,并新增一个 `grade` 属性。`Person` 类中有一个私有属性 `name` 和一个私有属性 `age`。注意,这里我们将 `name` 和 `age` 设置成私有的,因此在 `Child` 类中是不能直接访问它们的。在 `Child` 类中,我们定义了一个 `main` 方法,创建了一个 `Child` 对象并将其赋值给 `Person` 类型的变量 `p`。由于 `Child` 继承自 `Person`,因此可以将 `Child` 对象赋值给 `Person` 类型的变量。最后,我们在 `main` 方法中输出 `p.name` 的值,这里输出的是 `"Person"`。由于 `name` 是 `Person` 类的私有属性,因此子类中是不能直接访问它的。
相关推荐
![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)