条件 循环 嵌套 方法 递归结构

条件语句

  • java.lang 包中的 Math 类提供了一些用于数学计算的方
  • Math.random()该方法用于产生 0 到 1 区间的 double 类型的随机数,但是不包括 1。int i = (int) (6 * Math.random()); //产生:[0,5]之间的随机整数
if(条件语句){
}
else if(条件语句){
}
else{
}


switchcase标签){
	case1:
		语句;
		breakcase1:
		语句;
		breakdefault:
		语句;
}
public class square {
    public static void main(String[] args){
        double R = Math.random()*6;
        double area = 3.14*R*R;
        System.out.println(area);
        double circle = 2*3.14*R;
        System.out.println(circle);
        if(area>circle){
            System.out.println("面积大于周长");
        }
        else if(area==circle){
            System.out.println("面积等于周长");
        }
        else {
            System.out.println("面积小于周长");
        }
    }
}

循环语句

  1. break continue
  2. if语句
  3. 读取键盘输入在这里插入图片描述
import java.util.Scanner;

public class Salary {

	public static void main(String[] args) {
		
		Scanner s = new Scanner(System.in);
		
		double yearSalary;
		
		while(true) {
			System.out.print("请输入你的月薪:");
			double salary = s.nextDouble();
			
			System.out.print("请输入一年多少薪:");
			int severalMonth = s.nextInt();
			
			s.nextLine(); //用于读取上一行的的nextInt读取之后剩下的换行符
			
			System.out.print("请输入你的命令:(exti退出  next继续		任意字符继续)");
			String command = s.nextLine();
			
			if("exti".equals(command)) {
				System.out.println("结束");
				break;
			}
			else if("next".equals(command)) {
				System.out.println("继续");
				continue;
			}
			else {
				yearSalary = salary*severalMonth;
				System.out.println("您的年薪是:"+yearSalary);
				if(yearSalary>100000 & yearSalary<200000) {
					System.out.println("恭喜你年薪超过90%的国人");
				}
				else if(yearSalary>=200000){
					System.out.println("恭喜你年薪超过98%的国人");
				}
			}
		}
	}

}

循环嵌套

1. 正着的三角形
2. 倒着的三角形
3. 5*5方阵
4. 方法
public class Work {
	
	public static void nnTable() {
		//乘法表
		for(int i=1;i<=9;i++) {
			for(int j=1;j<=i;j++) {
				System.out.print(""+i+"*"+j+"="+(i*j)+"		");
			}
			System.out.println();
		}
	}
	
	public static void fnnTable() {
		//倒三角
		for(int i=9;i>0;i--) {
			for(int j=1;j<=i;j++) {
				System.out.print(""+i+"*"+j+"="+(i*j)+"		");
			}
			System.out.println();
		}
	}
	
	public static void ffMat() {
		//5*5方针
		int b = 5;
		for(int i=0;i<=5;i++) {
			if(i%2==0) {
				for(b=5;b>=1;b--) {
					if(b%2==0) {
						System.out.print("*		");
					}
					else {
						System.out.print("#		");						
					}
				}
			}
			else {
				for(b=5;b>=1;b--) {
					if(b%2==0) {
						System.out.print("#		");
					}
					else {
						System.out.print("*		");						
					}
				}
			}
			System.out.println();
		}
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 5;
		
		while((a--)>0) {
			System.out.println(a); 
		}
		System.out.println("================================================================");
		nnTable();
		System.out.println("================================================================");
		fnnTable();
		System.out.println("================================================================");
		ffMat();
	}
	
}

方法

 1. **形式参数**:在方法声明时用于接收外界传入的数据
 2. **实参**:调用方法时实际传给方法的数据
 3. **返回值**:方法在执行完毕后返还给调用它的环境的数据
 4. **返回值类型**:事先约定的返回值的数据类型,如无返回值,必须指定为 void。
public class MyWay {

	public static void print() {
		System.out.println("阿聪");
	}
	
	public static int add(int n1, int n2) {
		int sum = n1 + n2;
		return sum;
	}
	
	public static void main(String[] args) {
		
		int number1 = 10;
		int number2 = 20;
		int sum;
		
		sum = add(number1, number2);
		System.out.println("sum = " + sum);
	
		print();
	}
}
1. 实参的数目、数据类型和次序必须和所调用的方法声明的形式参数列表匹配
2. return 语句终止方法的运行并指定要返回的数据
3. Java 中进行方法调用中传递参数时,遵循值传递的原则(传递的都是数据的副本):
	Ⅰ基本类型传递的是该数据值的 copy 值。
	Ⅱ引用类型传递的是该对象引用的 copy 值,但指向的是同一个对象

方法重载

  1. 不同的含义:形参类型、形参个数、形参顺序不同
  2. 只有返回值不同不构成方法的重载如:int a(String str){}与 void a(String str){}不构成方法重载
  3. 只有形参的名称不同,不构成方法的重载如:int a(String str){}与 int a(String s){}不构成方法重
public class WayReload {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/** 求和的方法 */
		public static int add(int n1, int n2) {
			int sum = n1 + n2;
			return sum;
		}
		// 方法名相同,参数个数不同,构成重载
		public static int add(int n1, int n2, int n3) {
			int sum = n1 + n2 + n3;
			return sum;
		}
		// 方法名相同,参数类型不同,构成重载
		public static double add(double n1, int n2) {
			double sum = n1 + n2;
			return sum;
		}
		// 方法名相同,参数顺序不同,构成重载
		public static double add(int n1, double n2) {
			double sum = n1 + n2;
			return sum;
		}
//		//编译错误:只有返回值不同,不构成方法的重载
//		public static double add(int n1, int n2) {
//			double sum = n1 + n2;
//			return sum;
//		}
//		//编译错误:只有参数名称不同,不构成方法的重载
//		public static int add(int n2, int n1) {
//			double sum = n1 + n2;
//			return sum;
//		}

} 

递归

  • 缺点:简单的程序是递归的优点之一。但是递归调用会占用大量的系统堆栈,内存耗用多,在递归调用层次多时速度要比循环慢的多,所以在使用递归时要慎重。
  • 阶乘
public class RecFactorial {
	static int b=5;
	public static int Fac1(int d){
		if(d==1) {
			return 1;
		}
		return d*Fac1(d-1);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Fac1(5));
	}
}

  • 斐波拉且数列计算
public class Fbnacci {
	public static int FbncS(int a) {
		if (a==1) {
			return 1;
		}
		else if(a==2){
			return 1;
		}
		return FbncS(a-1)+FbncS(a-2);
	}
	public static void times(int times) {
		for(int i=1;i<=times;i++) {
			System.out.println(FbncS(i));
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		times(10);
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值