java中常用到的语句
一、java中的逻辑语句
1.if语句
三种格式:
(1).
if(条件表达式)
{
执行语句;
}
(2)
if(条件表达式)
{
执行语句;
}
else
{
执行语句;
}
(3)
if(条件表达式)
{
执行语句;
}
else if (条件表达式)
{
执行语句;
}
……
else
{
执行语句;
}
部分练习的代码:
//需求1:根据用户定义的数值不同。打印对应的星期英文。
class IfDemo
{
public static void main (String [] args)
{
/*int x = 1;
if (x==1)
System.out.println("Today is Mondey");
else if (x==2)
System.out.println ("Today is Tsday");
else if(x==3)
System.out.println ("Today is San");
else
System.out.println ("NO DAY");
*/
//需求2:根据用于指定月份,打印该月份所属的季节。
//3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
int x =13;
if (x==3||x==4||x==5)
System.out.println("是春季!");
else if (x==6||x==7||x==8)
System.out.println("是夏季!");
else if (x==9||x==10||x==11)
System.out.println("是秋季!");
else if (x==12||x==1||x==2)
System.out.println("是冬季!");
else
System.out.println("输入的月份有误!");
if (x>12 || x<1)
System.out.println("输入的月份有误");
else if (x>=3 && x<=5)
System.out.println("是春季");
else if (x>=6 && x <=8)
System.out.println("是夏季");
else if (x>=9 && x <=11)
System.out.println("是秋季");
else
System.out.println("是冬季");
if语句的特点是每一句都是单挑语句,不管条件是什么,最终结果只有ture和flase。
2.switch语句
格式:
switch(表达式)
{
case 取值1:
执行语句;
break;
case 取值2:
执行语句;
break;
…...
default:
执行语句;
}
代码:
//使用switch case语句实现次功能,如下
class IfDemo
{
public static void main (String [] args)
{
switch (x)
{
case 3:
case 4:
case 5:
System.out.println("是春季");
break;
case 6:
case 7:
case 8:
System.out.println("是夏季");
break;
case 9:
case 10:
case 11:
System.out.println("是秋季");
break;
case 12:
case 1:
case 2:
System.out.println("是冬季");
default:
System.out.println("NO");
}
}
}
switch语句特点:
a,switch语句选择的类型只有四种:byte,short,int ,char。
b,case之间与default没有顺序。先执行第一个case,没有匹配的case执行default。
c,结束switch语句的两种情况:遇到break,执行到switch语句结束。
d,如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。
二、java中的循环语句
1.while格式:
while(条件表达式)
{
循环体(执行语句);
}
2.do while 格式:
do while语句格式:
do
{
执行语句;
}while(条件表达式);
代码:
//打印从1到5的数
class WhileDemo
{
public static void main (String []args)
{
int x = 1;
/*while ( x<5 )
{
System.out.println("x="+x);
x++;
}
*/
do
{
System.out.println("do x="+x);
x++;
}
while (x<0);
}
}
while 与 do while 语句的区别就在于:
while先判断while里面的条件是否为ture,为ture,while开始执行,不为ture,则停止语句;
do while 先执行一次语句,然后再判断条件是否为ture,为ture的话再执行一遍。
3.for循环格式:
for(初始化表达式;循环条件表达式;循环后的操作表达式)
{
执行语句;
}
代码:
//用forfor循环嵌套语句画长方形。
class ForForDemo
{
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();
}
System.out.println("-------------------------------------------");
for (int x=0; x<5; x++ )//此处x从开始循环。
{
for (int y=x;y<5;y++)//此处y的每次循环都是以x循环完后的结果开始循环的,如x循环一次后结果为2,那么y的循环就从2开始,相当于比x少循环一次。
{
System.out.print("*");
}
System.out.println();
}
//打印九九乘法表。
for (int x=1;x<=9;x++)
{
for (int y=1;y<=x;y++)
{
System.out.print(y+"*"+x+"="+x*y+"\t" );//\t :制表符
}
System.out.println();
}
}
}
注意:如果循环需要定义循环增量,用for语句更合适。
三、函数的定义
在java中,具有一定独立功能的代码快称之为函数。函数之间可以互相调用。
格式:
修饰符 返回值类型 函数名(参数类型 形式参数1,参数类型 形式参数2,)
{
执行语句;
return 返回值;
}
代码举例:
//定义一个功能,有返回值类型,用来计算两个int类型数的和。
class FunctionDemo
{
public static void main (String []args)
{
getResult(3,5);
getResult(3,4,20);
}
public static int getResult (int a ,int b)
{
System.out.println(a+b);
return a+b;
}
//定义一个功能,用来计算三个数的和,有返回值类型
public static int getResult(int a ,int b,int c)
{
System.out.println(a+b+c);
return (a+b)+c; //发现两个函数定义的功能都一样,只是参数列表不一样,所以此处可以使用重载,函数之间是可以互相调用的。
}
}
当定义的功能相同,但参与运算的参数列表内容不同。我们既可以抽取相同的功能再定义成新的功能,这称之为方法的重载。
函数的主要特点是提高了代码的复用性。
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。
当定义的功能相同,但参与运算的未知内容不同。
那么,这时就定义一个函数名称以表示起功能,方便阅读,而通过参数列表的不同来区分多个同名函数。