一点手写代码题目

时间:2022-04-03 04:03:36

 

给定日期,判断日期是星期几?

 
//    实现给定某日期,判断是星期几
     public static String getWeekday(String date){//必须yyyy-MM-dd
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");  
        SimpleDateFormat sdw = new SimpleDateFormat("E");  
        Date d = null;  
        try {  
            d = sd.parse(date);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }
        return sdw.format(d);
     }

给定日期,判断是一年的第几天

package Day8_06;

import java.util.*;

public class ClassTest {
    public static void main(String[] args) {
        System.out.print("请输入年:");
        Scanner s1 = new Scanner(System.in); //获取屏幕输入
        int Year = s1.nextInt();
        System.out.print("请输入月:");
        Scanner s2 = new Scanner(System.in);
        int Mon = s2.nextInt();
        System.out.print("请输入日:");
        Scanner s3 = new Scanner(System.in);
        int Day = s3.nextInt();
        int sum = 0;
        
        switch(Mon){
            case 12:sum += 30;
            case 11:sum += 31;
            case 10:sum += 30;
            case 9 :sum += 31;
            case 8 :sum += 31;
            case 7 :sum += 30;
            case 6 :sum += 31;
            case 5 :sum += 30;
            case 4 :sum += 31;
            case 3 :
                if((Year % 4 == 0 && Year % 100 != 0) || (Year % 100 == 0 && Year % 400 == 0)){
                    System.out.println("这一年是闰年!");
                    sum += 29;
                }
                else{
                    System.out.println("这一年是平年!");
                    sum += 28;
                }
            case 2 :sum += 31;
            case 1 :sum += Day;
        }
        System.out.println(Year + "年" + Mon + "月" + Day + "日,是这一年的第:" + sum + "天");
    }
}

 

手写一个冒泡算法:

public class poup{
      int[] arr={6,3,8,2,9,1};
    System.out.println("排序前数组为:");
    for(int num:arr){
      System.out.print(num+" ");
    }
    for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数
      for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次
        if(arr[j]>arr[j+1]){
          int temp=arr[j];
          arr[j]=arr[j+1];
          arr[j+1]=temp;
        }
      }
    } 
    System.out.println();
    System.out.println("排序后的数组为:");
     for(int num:arr){
       System.out.print(num+" ");
     } 
  }


}