1、用循环语句打印三角形的逐步分析
分析过程:
1、将同一行内的数据使用循环输出 ----- 输出对应的列和相应的换行符
2、输出不同的行要使用相同的代码,所以继续使用循环
3、在打印列的循环外再添加循环
外循环负责行,内循环负责列
注意:内循环循环完成后,要打印换行
class LoopDemo08
{
public static void main(String[] args){
/*System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("\n");// \n 换行符
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print();//与使用\n效果一样
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("\n");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
System.out.print("*");
*/
/*
//循环打印一行五列
for(int i = 0; i < 5; i++){
System.out.print("*");// *****
}
System.out.println();//换行
for(int i = 0; i < 5; i++){
System.out.print("*");// *****
}
System.out.println();//换行
for(int i = 0; i < 5; i++){
System.out.print("*");// *****
}
System.out.println();//换行
for(int i = 0; i < 5; i++){
System.out.print("*");// *****
}
*/
/*for(int j = 0; j < 4; j++){
for(int i = 0; i < 5; i++){
System.out.print("*");
}
System.out.println();
}
*/
/*打印三角形
*
**
***
****
*****
*/
for(int rows = 0; rows < 5; rows++){
//内循环的次数和外循环的rows有关
for(int clos = 0; clos <= rows; clos++){
System.out.print("*");
}
//换行
System.out.println();
}
}
}
2、笛卡尔积
总结:小九九的打印
1、打印9的笛卡尔积 ---- 换行 ---- 循环嵌套
2、找关系 ---- 内循环的次数和外循环的i的值一样
class LoopDemo09
{
public static void main(String[] args){
for(int i = 1; i < 4; i++){
for(int j = 1; j < 4; j++){
System.out.println(i + " * " + j + " = " + i * j);
}
}
/*打印结果:
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
*/
System.out.println("----------------------------");
for(int i = 1; i < 4; i++){
for(int j = 1; j < 4; j++){
System.out.print(i + " * " + j + " = " + i * j + " ");
}
System.out.println();
}
/*打印结果:
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
*/
System.out.println("-------------------------------");
for(int i = 1; i < 10; i++){
for(int j = 1; j < 10; j++){
System.out.print(i + "*" + j + "=" + (i * j) + "\t");// \t 是制表符
}
System.out.println();
}
/*打印结果:
1*1=1 1*2=2 ------------------ 1*9=9
2*1=2 2*2=4 ------------------ 2*9=18
---------------------------------------------------
---------------------------------------------------
9*1=9 9*2=18 ------------------- 9*9=81
*/
System.out.println("-----------------------------------");
for(int i = 1; i < 10; i++){
for(int j = 1; j < i + 1; j++){
System.out.print(j + "*" + i + "=" + (i * j) + "\t");// \t 是制表符
}
System.out.println();
}
/*打印结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
---------------------------------
-----------------------------------------
1*9=9 2*9=18 ----------------------------------9*9=81
*/
}
}
3、循环嵌套使用break和continue
break:跳出当前循环,循环结束
continue:结束本次循环,继续下一次循环
1、单独使用break,跳出当前所在循环
2、让break跳出外循环,可以使用循环标号
注意:break和continue后面不可以直接跟代码 ---- 不可以后缀逻辑上可以执行到的代码
class LoopDemo10
{
public static void main(String[] args){
for(int i = 1; i < 11; i++){
if(i >= 5){
//跳出当前循环
break;
}
System.out.println(i);// 1 2 3 4
}
System.out.println("--------------------------------");
for(int i = 1; i < 11; i++){
if(i == 5){
break;
}
System.out.println(i);// 1 2 3 4
}
System.out.println("--------------------------------");
for(int i = 1; i < 11; i++){
if(i == 5){
//跳出当前循环,继续下一循环
continue;
}
System.out.println(i);// 1 2 3 4 6 7 8 9 10
}
System.out.println("--------------------------------");
for(int i = 1; i < 10; i++){
for(int j = 1; j < 10; j++){
//当j == 5 时,跳出当前循环
if(j == 5){
break;
}
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
System.out.println("--------------------------------");
out:for(int i = 1; i < 10; i++){ //out: 外循环标号 in:内循环标号
in:for(int j = 1; j < 10; j++){
//当j == 5 时,跳出外循环
if(j == 5){
//跳出整个循环
System.out.println();//正确
break out;
//System.out.println();//错误 ---- 无法执行到
}
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();//此语句将不会执行到,因为j=5时跳出整个循环所以将其放到break out前面即可
}
System.out.println("--------------------------------");
out:for(int i = 1; i < 10; i++){
in:for(int j = 1; j < 10; j++){
if(j == 5){
//跳出整个循环,继续下一循环
System.out.println();
continue out;
}
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
}
}
}
4、练习
小芳的妈妈每天给她2.5元钱,她都会存起来,但是,
每当这一天是存钱的第5天或者5的倍数的话,她都会
花去6元,请问,经过多少天,小芳才可以存到100元钱。
class LoopDemo11
{
public static void main(String[] args){
double allMoney = 0;
int days = 0;
while(true){
allMoney += 2.5;
days++;
//如果days是5的倍数,allMoney -6
if(days % 5 == 0){
allMoney -= 6;
}
if(allMoney >= 100){
break;
}
}
System.out.println("money = " + allMoney);//money = 101.0
System.out.println("days = " + days);//days = 74
}
}