for循环的格式以及使用
什么是循环
循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时
for循环格式
for(初始化表达式语句;判断条件语句;控制条件语句) { 循环体语句; }
执行流程
- 执行初始化表达式语句
- 执行判断条件语句,看其返回值是true还是false
- 如果是true,就继续执行
- 如果是false,就结束循环
- 执行循环体语句;
- 执行控制条件语句
- 回到B继续。
代码演示
public class hh {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
System.out.println("你是最棒的");
}
}
}
当循环体里面有一条语句时,{}可以省略不写 我建议不用省略{}
演示
public class hh {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("豆果");
}
}
}
代码演示1—10偶数求和
public class hh {
public static void main(String[] args) {
int ou = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
ou = ou + i;
}
}
System.out.println(ou);
}
}
ps:求和之前要定义一个变量来接受数据
求水仙花数如何分析及其演示
分析:
- 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
- 举例:153就是一个水仙花数153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 = 1 + 125 + 27 = 153
- 100----999 首先确定了循环的范围
- 难点在于我如何把三位数的每一个位上的数组提取出来
- 对一个数取个十百位153%10=3,153/10%10=5153/100%10=5;
演示
public class hh {
public static void main(String[] args) {
int shuxianhua = 0;
for (int i = 100; i <= 999; i++) {
int ge=i/1%10;
int shi=i/10%10;
int bai=i/100%10;
int shuxianhua1=ge*ge*ge+shi*shi*shi+bai*bai*bai;
if (i==shuxianhua1){
System.out.println(i);
shuxianhua++;
}
}
System.out.println(shuxianhua);
}
}
while循环语句使用格式及其演示
while循环基本语句格式以及完整格式
while(判断条件语句) { 循环体语句; }
初始化条件语句;
while(判断条件语句) {
循环体语句;
控制条件语句;
}
执行流程
- 执行初始化条件语句;
- 执行判断条件语句,看其返回值是true还是false
- 如果是true,就继续执行如果是false,就结束循环
- 执行循环体语句;
- 执行控制条件语句
- 回到b继续。
演示
public class yanshi {
public static void main(String[] args) {
int i=1;
while (i<10){
System.out.println("你好");
i++;
}
}
}
用while循环语句输出1—10
演示
public class yanshi {
public static void main(String[] args) {
int i=1;
while (i<=10){
System.out.println(i);
i++;
}
}
}
求1-----100之间的偶数和奇数和
演示
public class sjl {
public static void main(String[] args) {
int i = 1;
int ou = 0;
int ji = 0;
while (i <= 100) {
if (i % 2 == 1) {
ji += i;
} else {
ou += i;
}
i++;
}
System.out.println( ou);
System.out.println( ji);
}
}
do…while的使用及其格式演示
格式
初始化条件语句; do { 循环体语句; 控制条件语句; }while(判断条件语句);
执行流程
- 执行初始化条件语句;
- 执行循环体语句
- 执行控制条件语句;
- 执行判断条件语句,看其返回值是true还是false
- 如果是true,就继续执行,如果是false,就结束循环
- 回到b继续。
代码演示
public class yanshi {
public static void main(String[] args) {
int i=1;
do {
System.out.println("你好");
i++;
}while (i<10);
}
}
for,while,do…while循环语句的区别
- do…while循环至少执行一次循环体。
- 而for,while循环必须先判断条件是否成立,然后决定是否执行循环体语句。
- 如果你想在循环结束后,继续使用控制条件的那个变量,用while循环,否则用for循环。不知道用for循环。
- 因为变量及早的从内存中消失,可以提高内存的使用效率。
- 建议优先考虑for循环,然后是while循环 ,最后是do…while循环
循环嵌套输入
展示输入三角形“*”
public class yanshi {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
输出展示
*
**
***
****
*****
控制跳转语句
continue的使用场景
- 在循环语句中
- 离开使用场景的存在是没有意义的
作用
- 跳出一次循环,执行下一步操作
return的作用
- return 就是退出一个方法。
- 结束当前方法
break的作用
- 结束单层循环
方法概述及其格式
方法:
方法就是完成特定功能的代码块
在很多语言中都有函数的定义,但是在咱们JAVA语言中,我们将其称之为方法。
方法的格式
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2 …){ 方法体; return 返回值; }
方法格式详细解释
- 修饰符 比较多,后面会详细介绍。目前使用 public static
- 返回值类型 用于限定返回值的数据类型
- 方法名 就是一个名称,它的存在是为了方便我们调用方法
- 参数类型 限定调用方法时传入参数的数据类型
- 参数名 是一个变量,接收调用方法时传入的参数
- 方法体 完成功能的代码
- return 结束方法以及返回方法指定类型的值
- 返回值 就是功能的结果,由return带回,带回给调用者
代码演示
public class fangfa {
public static void main(String[] args) {
ff();
}
public static void ff(){
System.out.println("你好");
}
}
方法的之求任意三个数之和调用
代码演示
import java.util.Scanner;
public class 求和 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数");
int shu1 = sc.nextInt();
System.out.println("请输入第二个数");
int shu2 = sc.nextInt();
System.out.println("请输入第三个数");
int shu3 = sc.nextInt();
int he = add(shu1, shu2, shu3);
System.out.println(he);
}
public static int add(int x, int y, int z) {
int he = x + y + z;
return he;
}
}
方法的注意事项
- 方法不调用不执行
- 方法与方法是平级关系,不能嵌套定义
- 方法定义的时候参数之间用逗号隔开
- 方法调用的时候不用在传递数据类型
- 如果方法有明确的返回值,一定要由return带回一个值
方法重载概述及其基本使用
方法重载
- 在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同,与返回值无关。
- 参数列表不同
- 参数个数不同
- 参数类型不同
演示
public class sjl {
public static void main(String[] args) {
int add = add(1, 3, 5);
}
public static int add(int a, int b) {
System.out.println("小鱼");
return a + b;
}
public static double add(int a, double b) {
System.out.println("小狗");
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
public static int add(int a, int b, int c, int d) {
return a + b + c + d;
}
}
ps;
允许一个类中,可以定义多个同名方法,只要他们的参数个数,或者参数类型不同即可.不拿返回值类型来区分,就拿形参个数或形参类型来区分