黑马程序员_Java基础语句学习

来源:互联网 发布:怎么查淘宝买家信誉 编辑:程序博客网 时间:2024/05/22 17:42


----------------------Android培训、Java培训、java学习型技术博客、期待与您交流! ----------------------

/*
判断语句:
 If语句
 格式:
 1、if(条件语句)   
 {       
  执行语句;     
 }       
 2、if(条件语句)    
 {       
  执行语句;    
 }       
 else      
 {       
  执行语句;    
 }        
 3、if(条件语句)
 {
  执行语句;
 }
 else if(条件语句)
 {
  执行语句;
 }
 ……
 else
 {
  执行语句;
 }
*/
//代码演示:
class IfDemo
{
 public static void main(String[] args)
 {
  /*
  使用if else语句来实现分段函数
  y= 2*x (x>=0)
  y= x (x<0)
  */
  int x=1,y;
  if(x>=0)
   y=2*x;
  else
   y=x;
  // y = (x>=0)? 2*x : x;
  System.out.println("y="+y);
 }
}
/*
注:其中if else结构可以简化成:变量 = (条件表达式)? 表达式1 : 表达式2 ;
如上面的代码中就可以简化为: y = (x>=0) ? 2*x : x ;
利:简化代码。
弊:因为是三元运算符,运算完必须要有一个结果。
*/

/*
选择语句:
 Switch语句:
 格式:
 switch(值)
 {
  case 取值1:
   执行语句;
   break;
  case 取值2:
   执行语句;
   break;
  ……
  default :
   执行语句;
   break;
 }
*/
//代码演示:
class SwitchDemo
{
 public static void main(String[] args)
 {
  //根据变量的值获取是一周的第几天
  int x = 4;
  switch(x)
  {
   case 1:
    System.out.println("星期一");
    break;
   case 2:
    System.out.println("星期二");
    break;
   case 3:
    System.out.println("星期三");
    break;
   case 4:
    System.out.println("星期四");
    break;
   case 5:
    System.out.println("星期五");
    break;
   case 6:
    System.out.println("星期六");
    break;
   case 7:
    System.out.println("星期日");
    break;
   default:
    System.out.println("错误数据");
    break;
  }
 }
}
/*
注:switch结构始终从第一个case语句开始选择,条件满足执行case后面的代码,然后break语句结束switch选择。
当所有的case语句都不满足条件是执行default语句,所以default语句不可省略。当default语句放在最后时,后面可以不用跟break语句。
*/

/*
循环结构:
 while结构:
  while(循环条件)
  {
   循环体;
  }
 do while结构:
  do
  {
   循环体;
  }while(循环条件);
 for循环结构:
  for(初始化表达式;循环条件;循环后操作表达式)
  {
   执行语句;
  }
*/
//代码演示:
class XunHuanDemo
{
 public static void main(String[] args)
 {
  //分别使用while、do while和for实现1到10的打印
  int x= 1,y = 1;
  //用while实现
  while (x<=10)
  {
   System.out.print(x+" ");
   x++;
  }
  System.out.println("while");
  //用do while实现
  do
  {
   System.out.print(y+" ");
   y++;
  }while(y<=10);
  System.out.println("do while");
  //用for实现
  for(int z=1; z<=10; z++)
  {
   System.out.print(z+" ");
  }
  System.out.println("for");
 }
}
/*
注:while、 do while和for的区别:
while语句是先测试条件再执行语句,条件不符后终止
do while语句是先执行语句再测试条件,条件不符后终止,所以do while循环至少执行一次
变量都有自己的作用于。对于for来讲,如果将控制循环的变量定义在for语句中,那么变量只在for循环中有效。当for循环结束那么变量也将会在内存中被释放。
所以,当循环需要定义变量控制循环时,用for更合适。
*/

//循环嵌套:
 //用for循环演示。
class XunHuanDemo2
{
 public static void main(String[] args)
 {
  //使用for循环打印九九乘法表。
  for(int x=1; x<=9;x++)
  {
   for(int y=1; y<=x; y++)
   {
    System.out.print(y+"*"+x+"="+y*x+" ");
   }
   System.out.println(); //仅用于换行
  }
/*
break关键字:
break的作用是跳出当前循环块(for、while、do while)或程序块(switch)。
在循环块中的作用是跳出当前正在循环的循环体。在程序块中的作用是中断和下一个case条件的比较。
continue关键字:
continue用于结束循环体中其后语句的执行,并跳回循环程序块的开头执行下一次循环,而不是立刻循环体。
*/
//代码演示:
  //定义while无限循环,从1开始打印,遇到3的倍数不打印,打印到1000结束循环不在打印。
  int y=0;
  while(true)
  {
   y++;
   if (y%3==0)
    continue;
   if(y==1000)
    break;
   System.out.println(y);
  }
 }
}

/*
函数:函数是定义在类中的具有特定功能的一段小程序,函数也成为方法。
函数格式:
 修饰符 返回值类型 函数名(参数类型 参数1,参数类型 参数2,……)
 {
  执行语句;
  return 返回值;
 }
注:当函数运算后没有返回值时,可以用void关键字来标识。void关键字代表着函数没有返回值。
当函数指定了非void返回值类型时,则在函数中必须有完备的return语句返回所需的返回值。
下面的函数是错误的:
boolean text(int x)
{
 if(x>0)
  return true;
}
因为当x<=0时没有相应的return语句。正确的代码:
boolean text(int x)
{
 if(x>0)
  return true;
 return false;
}

函数重载:在同一个类中,存在函数名相同但参数列表不同的同名函数。
例如下面的代码:int getMax(int a, int b)和int getMax(int a, int b, int c)就发生了重载。
注:函数重载于函数的返回值类型无关,至于函数的参数列表有关。
*/
class FunctionDemo
{
 public static void main(String[] args)
 {
  System.out.println(getMax(4,5));
  System.out.println(getMax(4,5,6));
 }
 public static int getMax(int a, int b)
 {
  if(a>=b)
   return a;
  return b;
 }
 public static int getMax(int a, int b, int c)
 {
  if(getMax(a,b)>=c)
   return getMax(a,b);
  return c;
 }
 public static int getMin(int a, int b)
 {
  if(a>=b)
   return b;
  return a;
 }
}



----------------------Android培训、Java培训、java学习型技术博客、期待与您交流! ----------------------


0 0
原创粉丝点击