博主碎碎念: 练习题是需要大家自己打的请在自己尝试后再看答案哦;
个人认为,只要自己努力在将来的某一天一定会看到回报,在看这篇博客的你,不就是在努力吗,所以啊,不要放弃,路上必定坎坷,但是成功后放眼望去,这将是青春很浓重的一笔
今日份励志文案:若结局非你所愿,请在尘埃落定前奋力一搏
加油!!!!!
目录
注意:博主的答案并不是唯一的,每道题都有很多种写法,感兴趣的可以自己尝试一下
如果看不懂博主的代码,博主个人认为,你对逻辑控制学的不是太扎实,建议看一下博主上一篇对于逻辑控制的讲解
题目:计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
1. 从上述表达式可以分析出
a. 该表达式主要由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);
- }
题目:输出一个整数的每一位,如:123的每一位是3,2,1
本题主要考虑,如何获取一个数字的每一位:
“ / ” 除法是保留整数
“ % ” 取余是保留余数
例如:
123 % 10 = 3
123/10=12 12%10=2
12/10=1 1%10= 1
代码如下:
答案
- public static void main1(String[] args) {
- Scanner scanner=new Scanner(System.in);
- System.out.println("请输入一个整数:");
- int n = scanner.nextInt();
- while (n != 0) {
- System.out.print(n % 10+" ");
- n /= 10;
- }
- }
题目:编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
这道题判断相等用到的是 equals () 判断相等,但是这个方法不适用于基本类型,基本类型的判断用的还是" == " 和" != "
答案
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
- int count = 3;
- System.out.print("请创建你的密码num=");
- String num=scanner.nextLine();
- while (count != 0) {
- System.out.println("请输入你的密码:");
- String password = scanner.nextLine();
-
- //if(password == "123") 这个判断相等是错误的
-
- if(password.equals(num)) {
- System.out.println("登录成功!");
- break;
- }else {
- count--;
- System.out.println("你还有"+count+" 次机会!");
- }
- }
- }
题目:求斐波那契数列的第n项。(迭代实现)
斐波那契数列定义为:1 1 2 3 5 8 13 21 我们可以看到,从第3项开始,都等于前一项+前一项的前一项的和。
3=1+2
5+2+3
13 = 5+8
我们可以先定义f1保存第一项的值,f2保存第2项的值,f3保存第3项的值。
每次算法一个f3,就同步更新f1和f2的值
答案
- public static int fib(int n) {
- if(n == 1 || n == 2 ) {
- return 1;
- }
- int f1 = 1;
- int f2 = 1;
- int f3 = 1;
- for (int i = 3; i <= n; i++) {
- f3 = f1+f2;
- f1 = f2;
- f2 = f3;
- }
- return f3;
- }
- public static void main(String[] args){
- Scanner scanner =new Scanner(System.in);
- System.out.print("请输入斐波那契数列的第n项:");
- int n= scanner.nextInt();
- int N=fib(n);
- System.out.print("斐波那契的第n项是"+N);
- }
- }
题目:在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。 并执行代码,求出结果
答案
- public static int sum(int a,int b) {
- return a+b;
- }
-
- public static double sum(double a,double b,double c) {
- return a+b+c;
- }
- public static void main(String[] args){
- Scanner scanner =new Scanner(System.in);
- System.out.print("请输入两个整数");
- int a=scanner.nextInt();
- int b=scanner.nextInt();
- int c=sum(a,b);
- System.out.print("请输入三个小数");
- double d=scanner.nextDouble();
- double e=scanner.nextDouble();
- double f=scanner.nextDouble();
- double g=sum(d,e,f);
- System.out.println(c);
- System.out.println(g);
- }
题目:在同一个类中定义多个方法:要求不仅可以求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 static void main(String[] args){
- Scanner scanner =new Scanner(System.in);
- System.out.print("请输入两个整数");
- int a=scanner.nextInt();
- int b=scanner.nextInt();
- int c=max(a,b);
- System.out.print("请输入三个小数");
- double d=scanner.nextDouble();
- double e=scanner.nextDouble();
- double f=scanner.nextDouble();
- double g=max(d,e,f);
- System.out.println(c);
- System.out.println(g);
- }
如果有解释的不对或者不清晰,如果可以从评论区指出,我一定会加以修改,万分感谢
最后麻烦大佬们动一下发财的小手一键三连,万分感谢