-------
android培训、
java培训、期待与您交流! ----------
//打印10条输出语句,如果while语句里面没有控制循环的条件就是无限
//循环 最简单的无限循环 while(true)
class WhileDemo
{
public static void main(String args[])
{
int x = 0;
while (x<10)
{
System.out.println("hello world");
x++;
}
}
}
do while循环体至少执行一次,下面直接学习for循环
class ForDemo1
{
public static void main(String args[])
{
for (int x=0;x<10 ;x++ )
{
System.out.println("hello world");
}
}
}
for循环最重要的是要知道那一部分需要参与循环,for嵌套打印矩形,外循环控制行数,内循环控制每一行的输出内容
打印一个五行五列的星号
<pre name="code" class="java">class ForDemo2
{
public static void main(String args[])
{
for (int x =0;x<5 ;x++ )
{
for (int y = 0 ; y<5;y++ )
{
System.out.print("*");
}
System.out.println();
}
}
}
break跳出循环continue结束本次循环,继续下次循环
打印一个等腰三角形
class ForDemo3
{
public static void main(String args[])
{
for (int x = 0;x<10 ; x++)
{
for (int y=x+1;y<10 ;y++ )
{
System.out.print(" ");
}
for (int y=0;y<x ;y++ )
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}
函数——由于许多功能可以多次使用,因此可以把这些功能写成一个函数、
函数的写法 修饰符 返回值类型 函数名(参数列表)
下面写一个两个数相加的函数, 函数只有被调用才执行,运行时先从主函数开始为入口
class FunctionDemo
{
public static void main(String args[])
{
int a = 5;
int b = 6;
int c = getsum(a,b);
System.out.println(c);
}
public static int getsum(int x,int y)
{
int sum= x+y;
return sum;
}
}
函数只能调用函数,不能在内部再定义函数 返回值类型为void时,return可以省略
函数重载,函数名字相同,参数不同。