java学习第三天

<span style="font-family: Arial, Helvetica, sans-serif;">java基本组成部分之语句</span>

三种基本的程序结构

顺序结构:从上到下代码依次执行。

选择结构:if 语句;switch 语句

</pre><p></p><p><span style="white-space:pre"></span>if语句的三种基本格式:</p><p><span style="white-space:pre"></span></p><pre name="code" class="java">		if()
		{
		}

		if()
		{
		}
		else
		{
		}

		if()
		{
		}
		else if()
		{
		}
		else
		{
		}

switch语句的基本格式

switch()
		{
			case 1:
				break;
			case 2:
				break;
			case 3:			//值为3、4都执行后面的语句
			case 4:
				break;  
			case 5:
				break;
			default:
				break;
		}

switch语句的特点

1.括号中表达式的值只能是byte、short、int、char4中(经常使用的是char和int两种)

2.case的值是没有顺序的,default语句总是最后执行。

3.switch语句有两种结束的方式:(1)通过break语句结束(2)运行到witch语句的大括号结束

会发现if语句和switch能实现相同的功能

选择使用switch语句的情况:int和char类型的数值判断

选择使用if语句的情况:区间bool值判断

if语句、switch语句举例:

class iftest
{
	public static void main(String[] args)
	{
		/*
		//根据用户输入的数字,打印对应的星期英文
		int x=2;
		//x = System.in.readln();
		//System.out.println("X="+x);
		if(x == 1)
		{
			System.out.println("mandy");
		}
		else if(x == 2)
		{
			System.out.println("tsd");
		}
		else if(x == 3)
		{
			System.out.println("tsd");
		}

		else if(x == 4)
		{
			System.out.println("tsd");
		}
		*/

		//根据用户输入的月份,打印月份所属的季节
		int X=4;

		if(X>=3 && X<=5);
		{
			System.out.println("chun");
		}
		if(X ==3 || X == 4 || X == 5)
		{
			System.out.println("chun");
		}
		else if(X == 6|| X == 7 || X == 8)
		{
			System.out.println("xia");
		}
		else if(X == 9 || X == 10 || X == 11)
		{
			System.out.println("qiu");
		}
		else if(X == 12 || X == 1 || X == 2)
		{
			System.out.println("dong");
		}
		else
		{
			System.out.println("error");
		}
		System.out.println("hello world!");
	}
} 


class switchtest
{
	public static void main(String[] args)
	{
		//用switch语言来实现输入月份,判断月份所在的季节。
		int x=4;
		switch(x)
		{
			case 3:
			case 4:
			case 5:
				System.out.println("chun");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("xia");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println("qiu");
				break;
			case 12:
			case 1:
			case 2:
				System.out.println("dong");
				break;
			default:
				System.out.println("error");
						
		}
		System.out.println("switch");
	}
}


循环结构:while语句;do-while语句;for语句

while语句:先判断,条件满足在执行循环体

while()

{
执行语句;

}

do while语句:先执行,在判断条件(无论条件是否成立do while至少执行一次)

do

{
}
while ();

for语句:

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

执行顺序:先初始化,再判断,循环体,i++;

for语句的小练习

</pre><pre name="code" class="java">class fortest
{
	public static void main(String[] args) 
	{
		/*
		求1-10的和
		*/
		int sum=0;
		for(int i=1;i<=10;i++)
		{
			sum = sum + i;
		}
		System.out.println("1——10和是"+sum);
		System.out.println("_________________________________________________");
		/*
		利用循环输出形如
		******
		******
		******
		******
		******
		******
		的图形
		*/
		for(int i=0;i<6;i++)
		{
			for(int j=0;j<6;j++)
			{
				System.out.print("*");
			}
			System.out.println();
		}

		System.out.println("_________________________________________________");

				/*
		利用循环输出形如
		******
		*****
		****
		***
		**
		*
		的图形
		*/
		for(int i=0;i<6;i++)
		{
			for(int j=i;j<6;j++)//j=i;循环变量j开始的值分别为0、1、2、、、保证每行少一个
			{
				System.out.print("*");
			}
			System.out.println();
		}

		System.out.println("_________________________________________________");

						/*
		利用循环输出形如
		*
		**
		***
		****
		*****
		******
		的图形
		*/
		for(int i=0;i<6;i++)
		{
			for(int j=0;j<=i;j++)//j<=i;随着i的值变化分别是就j<=0、j<=1、、、、j<=5;保证每行输出的*逐渐增加
			{
				System.out.print("*");
			}
			System.out.println();
		}
		System.out.println("Hello World!");
	}
}
for语句打印99乘法表:
		/*
		for语句打印99乘法表
		1*1=1
		1*2=2	2*2=4
		1*3=3	2*3=6	3*3=9
		*/
		for(int i=1;i<=9;i++)
		{
			for(int j=1;j<=i;j++)
			{
				System.out.print(j+"*"+i+"="+i*j+"\t");
			}
			System.out.println();

		}
结果:






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值