您的位置:首页 > 编程语言 > Java开发

JAVA学习笔记(四) - 循环语句

2015-03-21 08:44 405 查看
while循环

/*
* while循环
*  循环变量,可以控制循环次数。
*/
public class Test
{
public static void main(String[] args)
{
int count = 5;

while(count > 0)
{
System.out.println("Hello World " + count--);
}

System.out.println(count);
System.out.println("hahahhaa");
}
}


/*
* while循环
*  实现1~100之和
*/
public class Test
{
public static void main(String[] args)
{
//i表示循环变量,此时i不仅可以控制循环次数,i还可以表示具体数值。
int i = 1;

int sum = 0;//用于存放结果

while(i <= 100)
{
sum += i;//sum = sum + i;
i++;//循环变量自增
}

System.out.println(sum);
}
}


do..while循环

/*
* do..while循环
* while循环区别?
*/
public class Test
{
public static void main(String[] args)
{
//循环变量,主要用于控制循环。
int count1 = 0;
do
{
System.out.println("Hello World");
}while(count1 > 0);

//do..while循环,实现1~100之和
int i = 1;
int sum = 0;

do
{
sum += i;//sum = sum + i;
i++;
}while(i <= 100);

System.out.println(sum);
}
}


for循环

/*
* for循环
*/
public class Test
{
public static void main(String[] args)
{
for(int i = 0; i < 5; i++)
{
System.out.println("Hello World");
}

/*
* for语句实现1~100之和
*/
int sum = 0;

for(int i = 1; i <= 100; i++)
{
sum += i;
}

System.out.println(sum);

//      int i = 0;
//      for(System.out.println("表达式1"); i++ < 2; System.out.println("表达式3"))
//      {
//          System.out.println("循环体");
//      }
}
}


/*
* for注意点
*/
public class Test
{
public static void main(String[] args)
{
// 表达式1,2, 3可以有多个语句
for (int x = 0, y = 10; x < 5; x++, y++)
{

}

// for中三个表达式都可有可无
int i = 1;
int sum = 0;
for (; i <= 100;)
{
sum += i;
i++;
}
System.out.println(sum);

// 第二个表达式如果没有,表示永久为true
for (;;)
{
System.out.println("Hello World");
}

}
}


题目:打印输出1~100之间偶数的和

/*
* 题目:打印输出1~100之间偶数的和
*/
public class Test
{
public static void main(String[] args)
{
//while
int i1 = 1;
int sum1 = 0;

while(i1 <= 100)
{
if(i1 % 2 == 0)
{
sum1 += i1;
}

i1++;
}
System.out.println(sum1);

//for
int sum2 = 0;
for(int i2 = 1; i2 <= 100; i2++)
{
if(i2 % 2 == 0)
{
sum2 += i2;
}
}
System.out.println(sum2);
}
}


题目:打印输出1000~2050年中所有的闰年

/*
* 题目:打印输出1000~2050年中所有的闰年
*/
public class Test
{
public static void main(String[] args)
{
//while
int year = 1000;
while(year <= 2050)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
System.out.println(year);
}

year++;
}

//for
for(int y = 1000; y <= 2050; y++)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
System.out.println(year);
}
}
}
}


break语句

/*
* break
*  在循环中,一旦运行到break语句,则终止循环。
*/
public class Test
{
public static void main(String[] args)
{
//break
for(int i = 100; i > 0; i--)
{
if(i % 7 == 0)
{
System.out.println(i);
break;
}
}

int count = 10;
while(true)
{

if(count % 7 == 0)
{
break;
}

System.out.println(count--);
}

System.out.println("for外代码");
}
}


continue语句

/*
* continue
*  终止本次循环,进入下次循环。
*/
public class Test
{
public static void main(String[] args)
{
int i = 0;
while(i++ < 5)
{
if(i % 3 == 0)
{
continue;
}
System.out.println(i);
}
//通过continue,实现1~100之间偶数之和
int sum = 0;
int a = 1;
while(a <= 100)
{
if(a % 2 != 0)
{
a++;
continue;
}

sum += a;
a++;
}
System.out.println(sum);
}
}


嵌套循环

/*
* 嵌套循环
*/
public class Test
{
public static void main(String[] args)
{
for(char c = 'A'; c <= 'D'; c++)
{
for(int i = 0; i <= 3; i++)
{
System.out.print(c + "" + i + "\t");
}

System.out.println();//换行
}
}
}


要求,使用*打印输出一个直角三角形

/*
* 要求,使用*打印输出一个直角三角形
*
*/
public class Test
{
/*

*
**
***
****
*****
******
*******

*/
public static void main(String[] args)
{
//打印七行
for(int row = 0; row < 7; row++)
{
for(int col = 0; col <= row; col++)
{
System.out.print("*");
}

System.out.println();
}
}
}


打印等腰三角形

/*
5 - row
*   2*row - 1
***
*****
*******
*********

*/
public class Test
{
public static void main(String[] args)
{
for(int row = 1; row <= 5; row++)
{
//打印空格
for(int spa = 1; spa <= 5 - row; spa++)
{
System.out.print(" ");
}

//打印*
for(int col = 1; col <= (2 * row - 1); col++)
{
System.out.print("*");
}

System.out.println();
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息