判断循环的用法 if switch

来源:互联网 发布:学校网络设计方案 编辑:程序博客网 时间:2024/06/05 23:05
public class TestWeek {




//需求:根据用户输入的数据给出指定的日期  
// if小练习 构造方法
static void test01(int week){

if(week==1){
 System.out.println(week+"对应的是星期一");
 }
 if(week==2){
 System.out.println(week+"对应的是星期二");
 }
 if(week==3) {
 System.out.println(week+"对应的是星期三");
 }
 if(week==4){
 System.out.println(week+"对应的是星期四");  
 }
 if(week==5){
 System.out.println(week+"对应的是星期五");
 }
 if(week==6){
 System.out.println(week+"对应的是星期六");
 }
 if(week==7){
 System.out.println(week+"对应的是星期七");
 }
 else{
    System.out.println("没有这一天");

 }
}
static void test02(int month){
      // if循环
if(month==3 || month==4 || month==5 ){
 System.out.println(month+"月是春季");
 }
 else if(month==6 || month==7 || month==8){
 System.out.println(month+"月是夏季");
 }
 
 else if(month==9 || month==10 ||month==11){
 System.out.println(month+"月是秋季");
 }
 
 else if (month==12 || month==1 || month==2){
 System.out.println(month+"月是冬季");
 }
 else{
 System.out.println(month+"月不是季节");
 }
     
}
/*swithc(表达式)
 *  case 取值1: {
 *  执行语句
 *  }
 *  case 取值2: {
 *  执行语句
 *  }
 *  byte short int char(四种数据选择类型)
 */


static void Test03(){
  /*
      * 用户随机输出数据对应   星期
      *  switch语句数据类型 int byte short char(常用)
       */
  
double a= Math.random();//随机类型用double
int b =(int)(a * 6)+1;
  switch(b) {
  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:
  break;
 
  }


}


  public static void main(String [] args){
 
 test01(8);
      test02(13);
 Test03();
  }


}
0 0
原创粉丝点击