黑马程序员——从零开始学java程序(第三天)

------- 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可以省略

函数重载,函数名字相同,参数不同。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值