java学习—day_4

 switch语句
switch(表达式(表达式的取值时候有限定的byte,short,int ,char,还有枚举,string))
{
 case 值一:语句体;break;

 。。。。。

 dafault:语句体;break;
}
break 表示中断结束的意思,可以控制switch语句的结束;

demo:
根据键盘录入的一个数据,来判断是周几:
//
import java.util.Scanner;
class SwitchDemo
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number:");
  int n = sc.nextInt();
  switch(n)
  {
   case 1:System.out.println("mnday");break;
   case 2:System.out.println("tuesday");break;
   case 3:System.out.println("wednesday");break;
   default:System.out.println("wrong");break;
  }
 }
}
注意的问题:
/*
switch 语句的注意事项:
 A:case 语句后只能是常量,不能是变量;
 B:case 语句后面的值不可以相同;
  default可以省略,但不建议;
  特殊情况:
  clss可以把值固定,单选题
  A,B,C,D,
 C:case穿透
 D:switch 遇到break结束,到结尾也结束
*/

import java.util.Scanner;
class SwitchDemo
{
 public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter a number:");
  int n = sc.nextInt();
  switch(n)
  {
   case 1:System.out.println("mnday");//break;
   case 2:System.out.println("tuesday");break;
   case 3:System.out.println("wednesday");break;
   default:System.out.println("wrong");break;
  }
 }
}

//
/*
 模拟单项选择题:
 分析:
  A;
*/
import java.util.Scanner;
class SwitchDemo
{
 public static void main(String[] args)
 {
  //
  System.out.println("you like");
  System.out.println("65 jerry");
  System.out.println("66 jerry");
  System.out.println("67 jerry");
  System.out.println("68 jerry");
  
  Scanner sc = new Scanner(System.in);
  System.out.println("your choce");
  int choiceNumber = sc.nextInt();
  char choice = (char)choiceNumber;
  switch(choice)
  {
   case 'A':
    System.out.println("a");
    break;
   case 'B':
    System.out.println("b");
    break;
   case 'C':
    System.out.println("c");
    break;
   default:
    System.out.println("c");
    break;
  }
 }
}
switch和if语句的使用判定:
在做判断的时候有两种选择:switch,if
if语句的使用场景:
针对结果为boolean类型的,
针对一个范围的判定;
针对几个常量值的判定
switch语句的使用;
针对几个常量值得判断


循环语句的组成:
初始化语句,判断条件语句,循环体语句,控制条件语句
for(初始化语句;判断条件语句;控制条件语句)
{
 循环体语句;
}
注意事项,判断条件语句结果是boolean类型;
案例分析:
/*
 请在控制台输出10个helloword
*/
class Demo
{
  public static void main(String[] args)
  {
   for(int i = 0;i < 10;i++)
   {
    System.out.println("hello!");
   }
  }
}

/*
 请在控制台输出10-1
*/

class Loop
{
 public static void main(String[] args)
 {
  for(int i = 10;i>0;i--)
  {
   System.out.println(i);
  }
 }
}

/*
 输出1-10的和
*/

class Loop
{
 public static void main(String[] args)
 {
  int s = 0;
  for(int i = 0;i < 11;i++)
  {
   s = s + i;
  }
  System.out.println(s);
 }
}
/*
 求出阶乘
*/

class ForDemo
{
 public static void main(String[] args)
 {
  int h = 1;
  for(int i = 2;i <= 5;i++)
  {
   h *=i;
  }
  System.out.println(h);
 }
}
/*
 在控制台中输出100-999中的所有水仙花数
 分析:个位,十位,百位;
 假设数字a;
 个位(single digit)
 十位(tens digit )
 百位(hundreds digit)
 
*/
class  ForDemo
{
 public static void main(String[] args)
 {
  int singleDigit,tensDigit,hundredsDigit ; 
  for(int i = 100;i <= 999;i++)
  {
   hundredsDigit = i /10/10 % 10;
   tensDigit = i / 10 % 10;
   singleDigit = i % 10; 
   if(i == (singleDigit*singleDigit*singleDigit + tensDigit*tensDigit*tensDigit + hundredsDigit*hundredsDigit*hundredsDigit))
   {
    System.out.println(i);
   }
  }
  
 }
}
while循环;初始化语句;
while(判断语句)
{
循环体语句;
控制语句;
}

for 循环和while循环的区别,for循环的控制语句在循环结束之后就不能继续访问了,而while的可以访问。
do
{
 循环体

}
while()


三种循环语句的区别:
do....while  至少执行一次循环体
而for 和while有可能一次都未执行

循环的使用,优先使用for,其次是while,do while


循环的嵌套:
/*
 请输出一个4行5列的星星图案
*/
class Demo
{
 public static void main(String[] args)
 {
  for(int i = 0;i < 4;i++)
  {
   for(int j = 0;j<4;j++)
   {
    System.out.print('*');
   }
   System.out.println('*');
  }
 }
}
/*
 输出99乘法表
*/
class Demo
{
 public static void main(String[] args)
 {
  for(int i = 1;i <= 9;i++)
  {
   for(int j = 1;j <= i;j++)
   {
    System.out.print(j+"*"+i+"="+i * j);
    System.out.print('\t');
   }
   System.out.println();
  }
 }
}

跳转控制语句:
goto保留字,java中用不了;
java中提供了break,continue,return
/*
 控制跳转语句:
 break
 使用场景,switch语句,循环语句加入了if判断的情况,跳出单层循环
 如何跳出多层循环:使用带标签的语句
 continue
 return
*/
class Demo
{
 public static void main(String[] args)
 {
  //定义带标签的语句
  first:for(int i=0;i<10;i++)
  {
   second:for(int j=0;j<10;j++)
   {
    third:for(int z=0;z<12;z++)
    {
     if(z == 3)
      break second;
    }
   }
   System.out.println("hello");
  }
 }
}
/*
 continue的使用 break是跳出整个循环,而continue是跳出单层循环
 return 不是为了跳出循环体,而是为了跳出方法,结束方法
*/
class Demo
{
 public static void main(String[] args)
 {
  for(int i=0;i<=10;i++)
  {
   if(i == 5)
   {
    System.out.println("ok");
   return;
   }
   System.out.println(i);
  }
  System.out.println("break");
 }
}

/*
 练习题
 小芳的妈妈每天给他2.5元,他都会存起来,每当这一天是存钱的第五天或倍数时,
 他都会花去6元钱,问经过多少天存到100元
*/
class Demo
{
 public static void main(String[] args)
 {
  double dayMoney = 2.5;
  double daySum = 0;
  int dayCount = 1;
  while(true)
  {
   daySum += dayMoney;
   if(daySum >= 100)
   {
    System.out.println(dayCount+"天");
    break;
   }
   if(dayCount%5 == 0)
   {
    daySum -= 6;
    System.out.println("第"+dayCount+"天,花了6元");
    
   }
   dayCount ++;
  }
  
 }
}

你可能感兴趣的:(java学习之路)