今天课程主要是讲解了周六布置的难题,我个人认为题目很难,花了周六周日的时间也没将作业全部独自完成.但老师以讲解就发现自己的思维被局限的很严重.要珍惜有头发的日子啊.今天也学习了for循环,感觉比起while循环更简洁一些,继续加油吧!!!!
笔记
#### 循环
1.for循环
for ( ; ; ) {
}
for 循环的优势:
1.for语句之后的小括号内容,可读性更强
2.可以通过for之后小括号,里面的内容大概推算出整个循环会执行多少次
3.for循环和一些特殊的数据关系非常紧密。
class Dome1 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
System. out. println ( i) ;
}
}
}
class Dome2 {
public static void main ( String[ ] args) {
for ( int i = 2 ; i <= 100 ; i += 2 ) {
System. out. println ( i) ;
}
}
}
class Dome3 {
public static void main ( String[ ] args) {
for ( char ch = 'A' ; ch <= 'Z' ; ch += 1 ) {
System. out. println ( ch) ;
}
}
}
2.break
字面含义:
跳出,打破
代码中的功能是跳出循环结构或者switch case结构
break关键字可以在循环过程中,当前循环结果出现了预期的内容,终止循环。
class Dome4 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 100 ; i++ ) {
System. out. println ( i) ;
if ( 50 == i) {
System. out. println ( "循环跳出" ) ;
break ;
}
}
}
}
class Dome5 {
public static void main ( String[ ] args) {
int i = 0 ;
while ( i <= 100 ) {
i += 1 ;
System. out. println ( i) ;
if ( 50 == i) {
System. out. println ( "循环跳出" ) ;
break ;
}
}
}
}
验证break跳出循环的关系
class Dome6 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 9 ; i++ ) {
System. out. println ( i) ;
for ( int j = 1 ; j <= 9 ; j++ ) {
System. out. println ( j) ;
}
}
}
}
3.continue
continue关键字
结束当前循环,进入下次循环
while,do while和continue 相爱相杀
class Dome7 {
public static void main ( String[ ] args) {
int i = 0 ;
while ( i < 10 ) {
System. out. println ( i) ;
i += 1 ;
if ( 5 == i) {
continue ;
}
}
}
}
for循环霸道总裁
class Dome8 {
public static void main ( String[ ] args) {
for ( int i = 1 ; i <= 9 ; i++ ) {
if ( 5 == i) {
System. out. println ( "13213" ) ;
continue ;
}
System. out. println ( "999999" ) ;
}
}
}