publicstaticbooleanproton(int a){if(a<2){
System.out.println("请重新输入不是0和1的数:");}if(a==2){
System.out.println("2是一个质子数!");returntrue;}if(a%2==0){//排除偶数returnfalse;}for(int i =3; i < a; i+=2){//排除奇数if(a%i==0){returnfalse;}}returntrue;}
for(int i =100; i <1000; i++){int a=i/100,b=i/10%10,c=i%10;if(i==(a*a*a+b*b*b+c*c*c)){
System.out.print("水仙花有:"+i);}}int random =(int) Math.random()*3;
System.out.println(random);
六、阶乘和
//求10!+9!+8!+…+1! = ?
//分析 i =10,j=10,i++,i*i,求的是和int sum=0;外层循环10次,变量出生10,内存循环,变量随外层变量递减,10.9.8.7.。。。3.2.1
int temp=0;for(int i =10; i >=1; i--){int sum1=1;for(int i1 =i; i1>=1; i1--){
sum1*=i1;}
temp+=sum1;}
System.out.println(temp);//orint re =0;for(int i =1;i<=10;i++){int r =1;for(int j =1;j<=i;j++){
r = r*j ;}
re = re + r;}
System.out.println("1!+2!+3!+4!+5!+6!+7!+8!+9!+10!="+re);
七、平闰年
//接收用户输入的年份year和月份month,计算该年该月的天数,并输出
//输入 year 年数不定 month 月数 12个;1、3、5、7、8、10、12有31天,4、6、9、11有30天,平年 2/28/闰年 2/29;
Scanner scanner =newScanner(System.in);int days=0;
System.out.println("请输入年份:");int year = scanner.nextInt();
System.out.println("请输入月份:");int month = scanner.nextInt();if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days=31;}elseif(month==4||month==6||month==9||month==11){
days=30;}else{if(year %4==0&& year %100!=0|| year %400==0){
days=29;}else{
days=28;}}
System.out.println(year+"年的"+month+"月共"+days+"天");
scanner.close();
Scanner scanner =newScanner(System.in);
System.out.println("输入年数!");int yaer = scanner.nextInt();
System.out.println("输入月份:");int month = scanner.nextInt();int days=0;switch(month){case1:case3:case5:case7:case8:case10:case12:
days =31;
System.out.println("这个月"+ days +"天");break;case4:case6:case9:case11:
days =30;
System.out.println("这个月"+ days +"天");break;case2:if(yaer%4==0&& yaer%100!=0|| yaer%400==0){
days=29;break;}else{
days=28;break;}}
八、九九乘法表
for(int i =1; i <=9; i++){for(int j =1; j<=i; j++){
System.out.print(i +"*"+ j +"="+ i * j +"\t");}
System.out.println("");
文章目录一、错题归纳:质子数二、递归三、最大公约数与最小公倍数四、鸡兔同笼五、水仙花数六、阶乘和七、平闰年八、九九乘法表一、错题归纳:质子数练习题:求质数:接收用户输入的数字,判断是否为质数质数的概念:一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数,也称为素数规定:1既不是质数,也不是合数 Scanner sc = new Scanner(System.in); System.out.println("请输入数:"); int a= sc.