计算指定日期是这一年的第几天
/*
计算给定日期是这一年的第几天
*/
import java.util.Scanner;
public class DayCal{
public static void main(String args[]){
int year;
int month;
int day;
boolean isLeap = false;
int sumDays = 0;
Scanner scan = new Scanner(System.in);
System.out.println("请输入年份:");
year = scan.nextInt();
System.out.println("请输入月份:");
month = scan.nextInt();
System.out.println("请输入日:");
day = scan.nextInt();
//判断是不是闰年
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
isLeap = true;
//计算天数
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
if(isLeap)
sumDays += 29;
else
sumDays += 28;
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.printf("%d年%d月%d日是这一年的第%d天\n", year, month, day, sumDays);
}
}
倒着写月份,妙啊!!!!
统计100000以内质数的个数
/*
统计100000以内质数的个数
附带计算下总时间
*/
public class PrimerTest{
public static void main(String args[]){
int count = 0;
long start = System.currentTimeMillis();
label: for (int i = 2; i <= 100000; i++)
{
for (int j = 2; j <= Math.sqrt(i); j++)
{
if(i % j == 0)
continue label;
}
count++;
}
long end = System.currentTimeMillis();
System.out.printf("100000内共%d个质数,花费%dms\n", count, end-start);
}
}
- System.currentTimeMillis()方法的使用
- continue/break使用标签跳出多层
螺旋矩阵
package stu.zdkk.java;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @Description 螺旋矩阵
* 从键盘输入一个整数(1~20)
则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。例如: 输入数字2,则程序输出:
1 2
4 3
输入数字3,则程序输出:
1 2 3
8 9 4
7 6 5
输入数字4, 则程序输出:
1 2 3 4
12 13 14 5
11 16 15 6
* @author zdkk Email: 1040893382@qq.com
* @version
* @date 2021年1月18日下午6:49:05
*/
public class PracticeTest7 {
public static void main(String[] args) {
int num = new Scanner(System.in).nextInt();
int arr[][] = new int[num][num];
int k = 1;//向右,=2向下,=3向左,=4向上
int i = 0;//i控制转圈个数 i∈[0,num)
int j = 0;//j控制每条边赋值元素个数
for(int m=1; m<=num*num; m++)
{
if(k == 1)
{
//向右
if(j<num && arr[i][j] == 0)
{
arr[i][j++] = m;
}
else
{
k = 2;
i++;
j--;
m--;
}
}
else if(k==2)
{
//向下
if(i<num && arr[i][j] == 0)
{
arr[i++][j] = m;
}
else
{
k = 3;
i--;
j--;
m--;
}
}
else if(k==3)
{
//向左
if(j>=0&&arr[i][j]==0)
{
arr[i][j--] = m;
}
else
{
k = 4;
i--;
j++;
m--;
}
}
else if(k==4)
{
//向上
if(i>=0&&arr[i][j]==0)
arr[i--][j] = m;
else
{
k=1;
i++;
j++;
m--;
}
}
}
// Arrays.toString(arr);
for (int m = 0; m < arr.length; m++) {
for (int n = 0; n < arr[m].length; n++) {
System.out.print(arr[m][n] + "\t");
}
System.out.println();
}
}
}