循环结构概述和for语句的格式及其使用
/*
* A:循环结构的分类
* for,while,do...while
* B:循环结构for语句的格式:
*
for(初始化表达式;条件表达式;循环后的操作表达式) {
循环体;
}
* C执行流程:
* a:执行初始化语句
* b:执行判断条件语句,看其返回值是true还是false
* 如果是true,就继续执行
* 如果是false,就结束循环
* c:执行循环体语句;
* d:执行循环后的操作表达式
* e:回到B继续。
* D:案例演示
* 在控制台输出10次"helloworld"
*/
class Demo1_For {
public static void main(String[] args) {
//在控制输出10次helloworld,这样做不推荐,因为复用性太差
/*System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");
System.out.println("helloworld");*/
for (int i = 1;i <= 10 ;i++ ) {
System.out.println("helloworld");
}
}
}
/*
* A:案例演示
* 需求:请在控制台输出数据1-10
* 需求:请在控制台输出数据10-1
* B:注意事项
* a:判断条件语句无论简单还是复杂结果是boolean类型。
* b:循环体语句如果是一条语句,大括号可以省略;如果是多条语句,大括号不能省略。建议永远不要省略。
* c:一般来说:有左大括号就没有分号,有分号就没有左大括号
*/
class Test1_For {
public static void main(String[] args) {
for (int i = 1;i <= 10 ;i++ ){
System.out.println("i = " + i);
}
System.out.println("-----------------------");
for (int i = 10;i >= 1 ;i-- ) {
System.out.println("i = " + i);
}
}
}
/*
* A:案例演示
* 需求:求出1-10之间数据之和
* 需求:求出1-100之间偶数和
* 需求:求出1-100之间奇数和
分析:1-10数据的和
0 + 1
1 + 2
3 + 3
6
...
*/
class Test2_For {
public static void main(String[] args) {
//1-10的和
/*int sum = 0;
for (int i = 1;i <= 10 ;i++ ) {
sum = sum + i;
}
System.out.println("sum = " + sum);*/
//1-100的偶数和
/*int sum = 0;
for (int i = 1;i <= 100 ;i++ ) {
if (i % 2 == 0) {
sum = sum + i;
}
}
System.out.println("sum = " + sum);*/
//1-100的奇数和
int sum = 0;
for (int i = 1;i <= 100 ;i+=2 ) {
/*if (i % 2 != 0) {
sum = sum + i;
}*/
sum = sum + i;
}
System.out.println("sum = " + sum);
}
}
水仙花数
/*
* A:案例演示
* 需求:在控制台输出所有的”水仙花数”
* 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
* 举例:153就是一个水仙花数。
* 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
分析:
1,100 - 999
2,获取每一个位数的值,百位,十位,个位
3,判断各个位上的立方和是否等于这个数,如果等于打印
*/
class Test3_Flower {
public static void main(String[] args) {
for (int i = 100;i <= 999 ;i++ ) { //获取100到999之间的数
int ge = i % 10; //123 % 10
int shi = i / 10 % 10; //12 % 10;
int bai = i / 10 / 10 % 10; //1 % 10
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i);
}
}
}
}
/*
* A:案例演示
* 需求:统计”水仙花数”共有多少个
分析:
1,需要有一个变量记录住水仙花数的个数
2,获取到所有的3位数
3,判断是否满足水仙花数
4,如果满足条件,计数器就自增
*/
class Test4_FlowerCount {
public static void main(String[] args) {
int count = 0;
for (int i = 100;i <= 999 ;i++ ) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 10 / 10;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
count ++; //满足条件就自增,计数器思想
}
}
System.out.println(count);
}
}
循环结构while语句的格式和基本使用
初始化语句;
while(判断条件语句) {
循环体语句;
控制条件语句;
}
class Demo1_While {
public static void main(String[] args) {
int x = 1;
while (x <= 10) {
System.out.println("x = " + x);
x++;
}
}
}
求1-100
//求1-100之和
int sum = 0;
int i = 1;
while (i <= 100) {
sum += i; //sum = sum + i;
i++; //让变量i自增
}
水仙花
int count = 0; //计数器
int i = 100;
while (i <= 999) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
count ++;
}
i++;
}
System.out.println("count =" + count);
循环结构do...while语句的格式和基本使用
初始化语句;
do {
循环体语句;
控制条件语句;
}while(判断条件语句);
int i = 11;
do {
System.out.println("i = " + i);
i++;
}
while (i <= 10);
/*
* A:案例演示
* 需求:请输出一个4行5列的星星(*)图案。
*
如图:
*****
*****
*****
*****
注意:
System.out.println("*");和System.out.print("*");的区别
* B:结论:
* 外循环控制行数,内循环控制列数
*/
class Demo1_ForFor {
public static void main(String[] args) {
for (int i = 1;i <= 4 ;i++ ) { //外循环决定的是行数
for (int j = 1;j <= 5 ;j++ ) { //内循环决定的是列数
System.out.print("*");
}
System.out.println();
}
}
}
/*
需求:请输出下列的形状
*
**
***
****
*****
*/
class Demo2_ForFor {
public static void main(String[] args) {
for (int i = 1;i <= 5 ; i++) { //外循环决定行数
for (int j = 1;j <= i ;j++ ) { //内循环决定列数
System.out.print("*");
}
System.out.println(); //将光标换到下一行的行首
}
}
}
/*
* A:案例演示
* 需求:在控制台输出九九乘法表。
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
...
*
**
***
*/
class Demo3_For99 {
public static void main(String[] args) {
for (int i = 1;i <= 9 ;i++ ) { //行数
for (int j = 1;j <= i ;j++ ) { //列数
System.out.print(j + "*" + i + "=" + (i * j) + "\t" );
}
System.out.println();
}
}
}
注意:
'\x' x表示任意,\是转义符号,这种做法叫转移字符。
'\t' tab键的位置
'\r' 回车
'\n' 换行
'\"'
'\''
控制跳转语句break语句
/*
* A:break的使用场景
* 只能在switch和循环中
*/
class Demo1_Break {
public static void main(String[] args) {
for (int x = 1;x <= 10 ;x++ ) {
if (x == 4) {
break; //跳出循环
}
System.out.println("x = " + x);
}
}
}
控制跳转语句continue语句
/*
* A:continue的使用场景
* 只能在循环中
*/
class Demo2_Continue {
public static void main(String[] args) {
for (int x = 1;x <= 10 ;x++ ) {
if (x == 4) {
continue; //终止本次循环,继续下次循环
}
System.out.println("x = " + x);
}
}
}
public class test3 {
public static void main(String[] args) {
outer: for (int i = 1;i <= 10 ;i++ ) { //a就是标号,只要是合法的标识符即可
System.out.println("i = " + i);
inner: for (int j = 1;j <= 10 ;j++ ) {
System.out.println("j = " + j);
break outer;
}
}
}
}
/*i = 1
j = 1*/
控制跳转语句return语句
class Demo4_Return {
public static void main(String[] args) {
for (int i = 1;i <= 10 ;i++ ) {
if (i == 4) {
//break; //停止循环
return; //返回的意思,用来返回方法
}
}
System.out.println("循环结束了");
}
}
A:return的作用
返回
其实它的作用不是结束循环的,而是结束方法的。
return和break以及continue的区别?
return是结束方法
break是跳出循环
continue是终止本次循环继续下次循环
方法概述和格式说明
A:为什么要有方法
提高代码的复用性
B:什么是方法
完成特定功能的代码块。
C:方法的格式
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {
方法体语句;
return 返回值;
}
D:方法的格式说明
修饰符:目前就用 public static。后面我们再详细的讲解其他的修饰符。
返回值类型:就是功能结果的数据类型。
方法名:符合命名规则即可。方便我们的调用。
参数:
实际参数:就是实际参与运算的。
形式参数;就是方法定义上的,用于接收实际参数的。
参数类型:就是参数的数据类型
参数名:就是变量名
方法体语句:就是完成功能的代码。
return:结束方法的。
返回值:就是功能的结果,由return带给调用者。
public static int add(int a,int b) { //方法
int sum = a + b;
return sum; //如果有返回值必须用return语句带回
}
/*
盘子 炒菜(油,调料,米,菜) {
炒菜的动作
return 一盘菜;
}
*/
方法重载概述和基本使用
/*
重载:方法名相同,参数列表不同,与返回值类型无关
重载的分类
1,参数个数不同
2,参数类型不同
顺序不同
*/
class Demo4_Overload { //overload重载
public static void main(String[] args) {
double sum1 = add(10,20.1);
System.out.println(sum1);
int sum2 = add(10,20,30);
System.out.println(sum2);
double sum3 = add(12.3,13);
System.out.println(sum3);
}
/*
求两个整数的和
1,返回值类型int
2,参数列表 int a,int b
*/
public static double add(int a,double b) {
return a + b;
}
/*
求三个整数的和
1,返回值类型int
2,参数列表 int a,int b,int c
*/
public static int add(int a,int b,int c) {
return a + b + c;
}
/*
求两个小数的和
1,返回值类型double
2,参数列表 double a,double b
*/
public static double add(double a,int b) {
return a + b;
}
}
来源:https://www.cnblogs.com/hudj/p/7704550.html