1 Star 0 Fork 0

大边牧/Java-Study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
class5.java 3.15 KB
一键复制 编辑 原始数据 按行查看 历史
大边牧 提交于 2022-11-07 10:53 . 方法的使用(完结)
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class class5 {
public static void main6(String[] args) {
hannuota(1,'a','b','c');
System.out.println();
hannuota(2,'a','b','c');
System.out.println();
hannuota(3,'a','b','c');
}
public static void hannuota(int n,char pos1,char pos2,char pos3) {
if(n == 1) {
move(pos1,pos3);
return;
}
hannuota(n-1,pos1,pos3,pos2);
move(pos1,pos3);
hannuota(n-1,pos2,pos1,pos3);
}
public static void move(char pos1,char pos2) {
System.out.print(pos1+"—》"+pos2+" ");
}
public static void main(String[] args) {
int n = 5;
int ret = factor(n);
System.out.println("ret = " + ret);
}
public static int factor(int n) {
if (n == 1) {
return 1;
}
return n * factor(n - 1); // factor 调用函数自身
}
public static int getSum(int N){ // N是形参
return (1+N)*N / 2;
}
public static void main3(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += fac(i);
}
System.out.println("sum = " + sum);
getSum(10); // 10是实参,在方法调用时,形参N用来保存10
getSum(100); // 100是实参,在方法调用时,形参N用来保存100
}
public static int fac(int n) {
//计算 1! + 2! + 3! + 4! + 5!
System.out.println("计算 n 的阶乘中n! = " + n);
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
public static int fa(int n) {
int ret =1 ;
for (int j = 1; j <= n; j++) {
ret *= j;
}
return ret;
}
public static int sum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
// int ret =fa(i);
sum += fa(i);
}
return sum;
}
public static void main4(String[] args) {
System.out.println(sum(5));
}
public static void main2(String[] args) {
int a = 10;
int b = 20;
System.out.println("第一次调用方法之前");
int ret = add(a, b);
System.out.println("第一次调用方法之后");
System.out.println("ret = " + ret);
System.out.println("第二次调用方法之前");
ret = add(30, 50);
System.out.println("第二次调用方法之后");
System.out.println("ret = " + ret);
}
public static int add(int x, int y) {
return x + y;
}
public static double add(double x,double y){
return x + y;
}
public static float add(float x,float y) {
return x + y;
}
public static boolean isLeapYear(int year) {
if((0 == year % 4 && 0 != year % 100) || 0 == year % 400){
return true;
}else{
return false;
}
//return 代表方法的结束
}
public static void main1(String[] args) {
boolean a = isLeapYear(2022);
System.out.println(a);
int b = add(2,3);
System.out.println(b);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/big-border-animal-husbandry/java-study.git
git@gitee.com:big-border-animal-husbandry/java-study.git
big-border-animal-husbandry
java-study
Java-Study
master

搜索帮助