企业网站模板库,怎么做广告推广,网站 做英文 翻译 规则,wordpress 调用子分类目录
1.输出一个整数的每一位
2.判定素数
3.求最大值方法的重载
4.输出闰年
5.打印 X 图形
6.数字9 出现的次数
7.计算分数的值
8. 模拟登陆
9.使用函数求最大值 10.斐波那契数列 星光不负赶路人#xff0c;加油铁子们#xff01;#xff01;#xff01; 1…目录
1.输出一个整数的每一位
2.判定素数
3.求最大值方法的重载
4.输出闰年
5.打印 X 图形
6.数字9 出现的次数
7.计算分数的值
8. 模拟登陆
9.使用函数求最大值 10.斐波那契数列 星光不负赶路人加油铁子们 1.输出一个整数的每一位
题目
输出一个整数的每一位如123的每一位是321
思路
本题主要考虑如何获取一个数字的每一位
123 % 10 3
123/1012 12%102
12/101 1%10 1
代码如下
public class Main {public static void main(String[] args) {Scanner scan new Scanner(System.in);int num scan.nextInt();while(num ! 0){int n num % 10;num / 10;System.out.print(n );}}
} 2.判定素数
题目
给定一个数字判定一个数字是否是素数
第1种方法如果一个数字是素数那么就只能整除1和自己本身。
public static void main(String[] args) {Scanner scanner new Scanner(System.in);int n scanner.nextInt();int i;for (i 2;i n;i) {if(n%i 0) {System.out.println(n不是素数n);break;}}if(i n) {System.out.println(n 是素数);}
}
第2种方式任何一个数字n都可以写成 n a*b的形式。那么必然会有一个数字是小于等于n/2的。
public static void main(String[] args) {Scanner scanner new Scanner(System.in);int n scanner.nextInt();int i;for (i 2;i n/2;i) {if(n%i 0) {//System.out.println(n不是素数n);break;}}if(i n/2) {System.out.println(n 是素数);}
}
第3种方式任何一个数字n都可以写成 n a*b的形式。那么必然会有一个数字是小于等于根号n的。
public static void main(String[] args) {Scanner scanner new Scanner(System.in);int n scanner.nextInt();int i;for (i 2;i Math.sqrt(n);i) {if(n%i 0) {//System.out.println(n不是素数n);break;}}if(i Math.sqrt(n)) {System.out.println(n 是素数);}
} 注图片来自网络如有侵权请联系删除 3.求最大值方法的重载
题目
在同一个类中定义多个方法要求不仅可以求2个整数的最大值还可以求3个小数的最大值
思路
做这道题我们首先要明白重载如何实现
重载1.方法名相同2. 参数列表不同数据类型个数顺序3. 返回值无关 本题可以借助Java原生类Math当中的max方法求最大值当然也可以自己通过If else进行比较。
Math的使用 不需要导入相关的包
public static int max(int a,int b) {return Math.max(a,b);
}public static double max(double a,double b,double c) {double m Math.max(a,b);return Math.max(m,c);
}
也可以这样写
public class Main {public static void main(String[] args) {int a 10;int b 20;int ret1 max(a , b);System.out.println(ret1);double c 2.23;double d 1.32;double e 5.52;double ret2 max(c , d , e);System.out.println(ret2);}public static int max(int x, int y){return x y ? x : y;}public static double max(double x, double y, double z){return x y ? x z ? x : z : y z ? y : z;}
} 4.输出闰年
题目
输出 1000 - 2000 之间所有的闰年
思路
首先要搞明白什么是闰年简单的说就是能够被 4整除且能被100整除 的年份或者是能够被 400整除 的年份即为闰年
public static void main(String[] args) {for (int year 1000; year 2000 ; year) {if(year %4 0 year%100 ! 0 || year %4000) {System.out.println(year 是闰年!);}}
} 5.打印 X 图形
题目X形图案_牛客题霸_牛客网
思路
假设i代表行j代表列当ij 或者 ij1 n此时为星号。其余的都是空格。 import java.util.*;public class Main {public static void main(String[] args) {Scanner scan new Scanner(System.in);while(scan.hasNextInt()) {int n scan.nextInt();for(int i 0;i n;i) {for(int j 0;j n;j) {if(i j) {System.out.print(*);}else if( ij1 n) {System.out.print(*);}else{System.out.print( );}}System.out.println();}}}
} 注图片来自网络如有侵权请联系删除 6.数字9 出现的次数 题目
编写程序数一下 1到 100 的所有整数中出现多少个数字9
思路
本题主要考察个位的9怎么判断十位的9怎么判断另外99是两个9.
public static void main(String[] args) {int count 0;for (int i 1; i 100; i) {if(i % 10 9) {//判断个位的9 count;}if(i/10 9) {count;//判断十位的9}}System.out.println(count);
} 7.计算分数的值
题目
计算1/1-1/21/3-1/41/5 …… 1/99 - 1/100 的值 。
思路
1 从上述表达式可以分析出 该表达式主要由100项奇数项为正偶数项为负
2 设置一个循环从1~100给出表达式中的每一项1.0/i, 注意此处不能使用1否则结果全部为0 然后使用flag标记控制奇偶项奇数项为正偶数项为负然后将所有的项相加即可
public static void main(String[] args) {double sum 0;int flg 1;for (int i 1; i 100; i) {sum 1.0/i * flg;flg -flg;}System.out.println(sum);
} 8. 模拟登陆
题目
编写代码模拟三次密码输入的场景。 最多能输入三次密码密码正确提示“登录成功”,密码错误 可以重新输 入最多输入三次。三次均错则提示退出程序
思路
本题注意考察字符串怎么比较相同使用方法equals。
public static void main11(String[] args) {Scanner scanner new Scanner(System.in);int count 3;while (count ! 0) {System.out.println(请输入你的密码);String password scanner.nextLine();//if(password 123) { 这个判断相等是错误的具体原因后续String章节进行讲解if(password.equals(123)) {System.out.println(登录成功);break;}else {count--;System.out.println(你还有count 次机会);}}
} 注图片来自网络如有侵权请联系删除 9.使用函数求最大值
题目
创建方法求两个数的最大值max2随后再写一个求3个数的最大值的函数max3。
要求
在max3这个函数中调用max2函数来实现3个数的最大值计算
思路
本题比较简单重点在如何求出两个数的最大值
public static int max2(int a,int b) {return a b ? a:b;
}public static int max3(int a,int b,int c) {int max max2(a,b);return max c ? max : c;
}
public static void main(String[] args) {System.out.println(max3(2, 5, 1));
} 10.斐波那契数列
题目
求斐波那契数列的第n项。(迭代实现)
思路
斐波那契数列定义为1 1 2 3 5 8 13 21 我们可以看到从第3项开始都等于前一项前一项的前一项的和。3 12 、5 23 、13 58 。
我们可以先定义n1保存第一项的值n2保存第2项的值n3保存第3项的值。
每次算一个n3就同步更新n1和n2的值。
/*** 求菲薄那切数列的第n项* param n* return*/import java.util.Scanner;//求斐波那契数列的第n项。(迭代实现)
public class Main {public static void main(String[] args) {Scanner sc new Scanner(System.in);int n sc.nextInt();System.out.println(fib(n));}public static int fib(int n){if(n 1 || n 2){return 1;}int n1 1; // 1 1 2 3 5 8int n2 1;int n3 0;for(int i 3;i n; i){n3 n1 n2;n1 n2;n2 n3;}return n3;}
} 注图片来自网络如有侵权请联系删除
希望对大家有所帮助感谢观看 文章转载自: http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.gdljq.cn.gov.cn.gdljq.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.zcsyz.cn.gov.cn.zcsyz.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.tcfhs.cn.gov.cn.tcfhs.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.bxnrx.cn.gov.cn.bxnrx.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.mprtj.cn.gov.cn.mprtj.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.knscf.cn.gov.cn.knscf.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.dygqq.cn.gov.cn.dygqq.cn http://www.morning.nyqxy.cn.gov.cn.nyqxy.cn http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.rmtxp.cn.gov.cn.rmtxp.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.pcjw.cn.gov.cn.pcjw.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.bfgbz.cn.gov.cn.bfgbz.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.daxifa.com.gov.cn.daxifa.com http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.rcmcw.cn.gov.cn.rcmcw.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.sogou66.cn.gov.cn.sogou66.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.mpszk.cn.gov.cn.mpszk.cn