Java基础(循环,数组,程序调试)练习题

如果有一天,你偶然看到了这些文字,我希望这几分钟是真正属于你自己的,在这里你给自己加油,打气,继续去完成你曾经的梦想,勇敢的去挑战自己,历练自己!

01.程序调试题

E201_03 _01评委评分

题目:下面的程序存在错误,请调试并排除错误

public static void main(String[] args) {
    System.out.println("请依次输入5个评委的评分在(0-100):");
    Scanner scan = new Scanner(System.in);
    int score1 = scan.nextInt();
    int score2 = scan.nextInt();
    int score3 = scan.nextInt();
    int score4 = scan.nextInt();
    int score5 = scan.nextInt();

    int min = 100;
    if(score1 < min){
        min = score1;
    }
    if(score2 < min){
        min = score1;
    }
    if(score3 < min){
        min = score1;
    }
    if(score4 < min){
        min = score1;
    }
    if(score5 < min){
        min = score1;
    }
    int max = 0;
    if(score1 > max){
        max = score1;
    }
    if(score2 > max){
        max = score1;
    }
    if(score3 > max){
        max = score1;
    }
    if(score4 > max){
        max = score1;
    }
    if(score5 > max){
        max = score1;
    }
    int total = score1+score2+score3+score4+score5;
    float ave = (total-max-min)/5;

    System.out.println("");
}

错误列表:float ave = (total-max-min)/5;
正确代码:

public class HomeWork_Part3_01_PingWeiPingFen {
    public static void main(String[] args) {
        System.out.println("请依次输入5个评委的评分在(0-100):");
        Scanner scan = new Scanner(System.in);
        int score1 = scan.nextInt();
        int score2 = scan.nextInt();
        int score3 = scan.nextInt();
        int score4 = scan.nextInt();
        int score5 = scan.nextInt();

        int min = 100;
        if(score1 < min){
            min = score1;
        }
        if(score2 < min){
            min = score1;
        }
        if(score3 < min){
            min = score1;
        }
        if(score4 < min){
            min = score1;
        }
        if(score5 < min){
            min = score1;
        }
        int max = 0;
        if(score1 > max){
            max = score1;
        }
        if(score2 > max){
            max = score1;
        }
        if(score3 > max){
            max = score1;
        }
        if(score4 > max){
            max = score1;
        }
        if(score5 > max){
            max = score1;
        }
        int total = score1+score2+score3+score4+score5;
        float ave = (float) (total-max-min)/5;

        System.out.println(ave);
    }

}

运行结果:

请依次输入5个评委的评分在(0-100):
1
1
1
1
1
0.6

心得体会:在进行float ave = (total-max-min)/5;自动类型转换时,先进行算数运算再进行自动类型转换,导致结果误差较大。应先进行强制类型转换,再进行算术运算。

E201_03 _02判断水仙花数

题目要求:下面的程序存在错误,请调试并排除错误

public static void main(String[] args) {
    System.out.println("请输入一个三位数:");
    Scanner scan = new Scanner(System.in);
    int num = scan.nextInt();

    if(num<100 && num>999){
        System.out.println(num+"不是一个三位数");
        return;
    }

    int b100 = num/100;
    int b10 = num%100/10;
    int b1 = num%10;

    boolean result = b100*b100*b100+b10*b10*b10+b1*b1*b1 == num;
    if(result = true){
        System.out.println(num+"是水仙花数");
    }else{
        System.out.println(num+"不是水仙花数");
    }
}

错误列表
if(result = true){
System.out.println(num+“是水仙花数”);
}

正确代码:

public class HomeWork_Part3_02_PanDuanShuiXianHuaShu {
    public static void main(String[] args) {
        System.out.println("请输入一个三位数:");
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();

        if(num<100 && num>999){
            System.out.println(num+"不是一个三位数");
            return;
        }

        int b100 = num/100;
        int b10 = num%100/10;
        int b1 = num%10;

        boolean result = b100*b100*b100+b10*b10*b10+b1*b1*b1 == num;
        if(result == true){
            System.out.println(num+"是水仙花数");
        }else{
            System.out.println(num+"不是水仙花数");
        }
    }

}

运行结果:
请输入一个三位数:
999
999不是水仙花数
心得体会:书写代码时一定要注意“==”和“=”防止忘敲一个等号。

02.循环题

E201_04_01存钱翻倍

题目要求: 假定银行一年期存款利率为3%,到期后自动转存,问一万元需要连续存多少年才能超过2万元

思路:
(1) 一年后将获取的利息和本金作为本金参与下一年的利息计算,并记录年份。
(2) 当账户超过2万元时输出年份结果。
代码:

package HomeWork.Part4;

public class HomeWork_Part4_01_CunQian {
    public static final float CAPITAL = 10000f;

    public static void main(String[] args) {
        int years = 0;
        float totalMoney=CAPITAL;
        float midMoney =CAPITAL;
        while(totalMoney<=20000){
            totalMoney =midMoney+midMoney*0.03f;
            midMoney = totalMoney;
            years++;
        }
        System.out.println("需要连续存储"+years+"年!");
    }
}

运行结果:
需要连续存储24年!

心得体会:
搞清楚本金加利息作为下一年本金很重要。

E201_04_02统计法计算圆周率

题目要求:随机数法求解π,向正方形中扔点,如果落在圆内则计数,重复这个过程,然后用该计数除以总次数就是π的四分之一。

思路:
(1) 输入圆的半径,同时也为正方形的长。
(2)判断随机产生的两个点之间的距离是否超过1,未超过者计数,然后用该计数除以总次数就是π的四分之一。
(3)输出结果。

代码:

package HomeWork.Part4;

public class HomeWork_Part4_02_PaiDeZhi {
    public static final int COUNT=1000000;
    public static void main(String[] args) {
        float count=0;
        float result=0;
        for(int i=0;i<COUNT;i++){
            double x = Math.random();
            double y = Math.random();
            double distance = Math.sqrt(x*x+y*y);
            if(distance<=1){
                count++;
            }
        }
        result =count/COUNT;
        System.out.println(result);
    }
}

运行结果:
3.141768

心得体会:
将现实中的可操作的项目转换成数学模型让计算机可以用计算解决问题很重要。

E201_04_03_计算数列之和

题目要求:编程计算数列的前20项之和:1/2,2/3,3/5,5/8……
思路:
(1) 找规律,前一项的分母作为后一项的分子,前一项的分子与分母之和作为后一项的分母。
(2) 输入20.
(3) 输出结果。

代码:

package HomeWork.Part4;

public class
HomeWork_Part4_03_QianNXiangHe {
    public static void main(String[] args) {
        float num1=1f, num2=2f;
        float sum = 0,temp;
        for(int i =0;i<3;i++){
            sum = sum +num1/num2;
            temp = num1;//保存原来的分子
            num1 = num2;//分母
            num2 =temp +num2;
        }
        System.out.println(sum);
    }
}

运行结果:
12.278297

**心得体会:**找出问题的规律,根据规律解决问题。

E201_04_04_输出所有水仙花数

题目要求: 如题
思路:
(1) 水仙花数是一位三位数,因此我们只需要遍历三位数,并求出该三位数的各位数,再判断它的每个位上的数字的 3次幂之和等于它本身。
(2) 输出符合条件的结果。

代码:

package HomeWork.Part6;

import java.util.Scanner;

public class HomeWork_Part6_01_ShuiXianHuaShu {
    public static void main(String[] args) {
        System.out.println("请输入所求范围的最大值(三位数):");
        Scanner scan= new Scanner(System.in);
        int n  =scan.nextInt();
        countHua(n);
    }

    private static void countHua(int n) {
        System.out.println("水仙花数为:");
        for(int i=100;i<=n;i++){
            int num1=i%10;
            int num10=(i/10)%10;
            int num100=i/100;
            boolean result = num1*num1*num1+num10*num10*num10+num100*num100*num100==i;
            if(result==true){
                System.out.println(i);
            }
        }
    }

}

运行结果:

水仙花数为:
153
370
371
407

心得体会:
代码不同功能之间可以换行,让代码更加清晰。

E201_04_05验证角谷定理

题目要求:任意一个自然数,若为偶数则除以2,若为奇数则乘以3加1,得到新的自然数,经过若干次这样演算,最终结果必然为1。要求显示验证的步骤。
思路:
(1) 输入一个自然数。
(2) 判断自然数的奇偶,若为偶数则除以2,若为奇数则乘以3加1,循环操作使其最终结果为1。
(3) 输出中间的过程。
代码:

package HomeWork.Part4;

import java.util.Scanner;

public class HomeWork_Part4_05_JiaoGuDingLi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入一个自然数:");
        int num1 = scan.nextInt();

        //如果num1值为1
        if(num1 == 1){
            num1 = num1*3+1;
            System.out.println(num1+"\n"+2+"\n"+1);
            num1 = 1;
        }

        if(num1 == 0){
            System.out.println(0);
        }
        //判断奇偶数并作出相应判断
            while (num1 != 1 && num1 != 0) {
                if(num1%2 == 0) {
                    num1 = num1 / 2;
                    System.out.println(num1);
                }
                else{
                    num1 = num1*3+1;
                    System.out.println(num1);
                }
            }



    }
}

运行结果:
请输入一个自然数:
5
其过程为:
16
8
4
2
1

心得体会:
搞清楚如何步入下一个过程很重要。

E201_04_06 输出100以内的素数

题目要求:如题
思路:
(1) 输入所求素数范围的最大值100
(2) 判断该范围内每个数是否为素数(除1和自身外不能被其他数整除 ),并用标记标明其是否为素数。
(3) 输出结果。
代码:

package HomeWork.Part4;

import java.util.Scanner;

public class HomeWork_Part4_06_ShuChuSuShu {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你所求的素数的范围的最大值:");
        int n =scan.nextInt();
        //判断是否为素数n
        for(int i=2;i<n;i++){
            int flag = 1;//标记,当flag为1时表示i为素数。
            for(int j=2;j<Math.sqrt(i)+1;j++){
                if(i%j == 0 && i != 2){
                    flag = 0;
                    break;
                }
            }

            //输出结果
            if(flag==1){
                System.out.println(i);
            }
        }
    }
}

运行结果:
请输入你所求的素数的范围的最大值:
100
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
心得体会:
灵活运用标记,有时候有奇效。

E201_04_07九九乘法表

题目要求:输出九九乘法表

思路:
(1) 用一个数一次乘以从自身到九,并且该数从一到九开始乘其他数。
(2) 输出运算后的结果。
代码:

package HomeWork.Part4;

public class HomeWork_Part4_07_JiuJiuChenFaBiao {
    public static void main(String[] args) {
        for(int i=1;i<10;i++){
            for(int j=1;j<i+1;j++){
                int result =i*j;
                System.out.print(j+"x"+i+"="+result+" ");
            }
            System.out.println();
        }
    }
}
运行结果:
1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
心得体会:

有时需要灵活运用for循环

E201_04_08_输出1000以内的完数

题目要求:如题,所谓完数指的是一个数的因子之和(包括1但不包括本身)等于其本身。

思路:

(1) 输入所求范围的最大值。
(2) 计算除完数的因子,并将因子相加,判断是否为等于其本身,如果等于本身则输出结果。
(3) 输出结果。

代码:

package HomeWork.Part4;

import java.util.Scanner;

public class HomeWork_Part4_08_WanShu {
    public static void main(String[] args) {
        System.out.println("请输入所求范围的最大值:");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();

        for(int i=2;i<=n;i++){
            int sum =0;
            for(int j=1;j<i;j++){
                if(i%j==0){
                    sum += j;
                }
            }

            if(sum==i){
                System.out.println(i);
            }
        }
    }
}

运行结果:
请输入所求范围的最大值:
1000
完数为:
6
28
496
心得体会:将数学问题转换成计算机能计算的数学模型很重要。

03.数组

E201_05_01_成绩统计

题目要求:根据学生的考试成绩,计算最高分、最低分、平均分和标准差。

解题思路:
(1) 输入学生的各科成绩。
(2) 将各科成绩从低到高排名。
(3) 计算平均分和标准差。
(4) 输出结果。
代码:

package HomeWork.Part5;

import org.w3c.dom.ls.LSOutput;

import java.util.Scanner;
import java.util.Arrays;
public class HomeWork_Part5_01_ChenJiTongJi {
    public static final int N = 5;//科目
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请依次输入"+N+"门科目学生的考试成绩:");
        float [] scores = new float[N];
        for(int i=0;i<scores.length;i++){
            scores[i] = scan.nextFloat();
        }
        Arrays.sort(scores);

        float max = scores[scores.length-1];
        float min = scores[0];
        float sum = 0,ave;
        for(int i=0;i<scores.length;i++){
            sum = sum+scores[i];
        }
        ave = sum/N;
        float standardDeviation ;//标准差
        float standardDeviation1=0;//各科成绩与平均值之差的平方之和
        for(int i=0;i<scores.length;i++){
            standardDeviation1 += (float)Math.pow((scores[i]-ave),2);
        }
        standardDeviation =(float)Math.sqrt(standardDeviation1/N);
        System.out.printf("最大值为:%f,最小值为:%f,平均分%f,标准差%f",max,min,ave,standardDeviation);
    }
}

运行结果:
请依次输入5门科目学生的考试成绩:
100
100
100
100
99
最大值为:100.000000,最小值为:99.000000,平均分99.800003,标准差0.400000
心得体会:
耐心研究,终会有结果。

E201_05_02_数组交集

题目要求:计算两个集合的交集
解题思路:
(1) 输入两个集合的值。
(2) 让集合1中的值依次和集合2的值比较,相同则输出。
(3)输出结果。
代码:

package HomeWork.Part5;

import java.util.Scanner;

public class HomeWork_Part5_02_ShuZuJiaoJI {
    public static final int N = 5;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] arrays1 = new int[N];
        int[] arrays2 = new int[N];

//        输入两个集合的值
        System.out.println("请输入集合1的值:");
        for(int i=0;i<arrays1.length;i++){
            arrays1[i] = scan.nextInt();
        }
        System.out.println("请输入集合2得值:");
        for(int i=0;i<arrays2.length;i++){
            arrays2[i] = scan.nextInt();
        }

        //输出交集
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                if(arrays1[i]==arrays2[j]){
                    System.out.print(arrays1[i]+" ");
                }
            }
        }
    }
}

运行结果:
请输入集合1的值:
123
4
5
6
7
请输入集合2得值:
123
3
4
5
6
两集合的交集为:
123 4 5 6
心得体会:
将数学问题转换成计算机能计算的问题思想很重要。

E201_05_03_数组并集

题目要求:计算两个集合的并集

解题思路:
(1) 输入集合1和集合2
(2) 输出集合1的所有值,再将集合2的值跟集合1的值比较,如果找不到与集合1相同的值,即将该值输出。
(3) 输出结果。
代码:

package HomeWork.Part5;

import java.util.Scanner;

public class HomeWork_Part5_03_ShuZuBingJi {
    public static final int N = 5;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] arrays1 = new int[N];
        int[] arrays2 = new int[N];

//        输入两个集合的值
        System.out.println("请输入集合1的值:");
        for(int i=0;i<arrays1.length;i++){
            arrays1[i] = scan.nextInt();
        }
        System.out.println("请输入集合2的值:");
        for(int i=0;i<arrays2.length;i++){
            arrays2[i] = scan.nextInt();
        }

        //输出并集
        System.out.println("两集合的并集为:");
        for(int i=0;i<N;i++){
            System.out.print(arrays1[i]+" ");
            for(int j=0;j<N;j++){
                if(arrays2[i]==arrays1[j]){//如果找到相同的值,直接跳过该值
                    break;
                }
                else if(arrays2[i]!=arrays1[j]&&j==N-1){
                    System.out.print(arrays2[i]+" ");
                }
            }
        }
    }
}

运行结果:
请输入集合1的值:
1 2 3 4 5 7
请输入集合2的值:
1 3 4 6 8
两集合的并集为:
1 7 2 3 4 5 6
心得体会:
注意细节的东西。

E201_05_04_超大数相加

题目要求:长整型数据为32位,能表示的最大值为232-1,但是在很多情况下是不够用的,需要程序设计者自行解决这个问题。例如可以通过数组存储超长整数的每一位数值,对应位相加,如果超过9则产生进位。这里假设运算数据和结果都不超过30位。
解题思路:
(1) 输入两个超大数。
(2) 将超大数每一位存进数组中。因为计算的时候要从个位开始计算,并且还可能产生进位,所以考虑将值逆序存储于数组中进行计算。
(3) 从低位开逐位开始计算,结果位=对应的两位以及进位之和对10取余
(4) 进位为除10的商。
(5) 将结果输出,若最后一位没有产生进位,数组该位为0,不能将其输出,应该去除该影响。
代码:

package HomeWork.Part5;

import java.util.Scanner;

public class HomeWork_Part5_04_ChaoDaShu {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入两个超大数据:");
        String bigNum1 = scan.next();
        String bigNum2 = scan.next();
        bigNumSum(bigNum1,bigNum2);
    }

    //计算两数之和
    public  static void bigNumSum(String num1,String num2){
        int[] arrays1 = new int[30];
        int[] arrays2 = new int[30];
        int[] arrays3 = new int[30];//存储两数之和的结果

        //将值逆序存储于数组
        for(int i=0;i<num1.length();i++){
            arrays1[i]=num1.charAt(num1.length()-1-i)-'0';
        }
        for(int i=0;i<num2.length();i++){
            arrays2[i]=num2.charAt(num2.length()-1-i)-'0';
        }

        int n=0,x=0;//n循环变量,x为进位数
        //各位数对应相加并进位
        while(n<num1.length()||n<num2.length()){
            arrays3[n]=arrays1[n]+arrays2[n]+x;
            x = arrays3[n]/10;
            arrays3[n]%=10;
            n++;
        }

        arrays3[n]=x;
        //如果最后两个数值相加没有进位
        if(arrays3[n]==0){
            n--;
        }
        //逆序输出正确结果
        for(int i=n;i>=0;i--){
            System.out.print(arrays3[i]);
        }

    }
}

运行结果:
请输入两个超大数据:
12222222222222222222222
12222222222222222222229
24444444444444444444451
心得体会:
将两个数据每一位存入数组,再进行我们小学时学习的进位运算来解决超大数据算术问题,从中学习到我们平时接触的一些东西我们完全可以将其转换成计算机来解决。因此将问题转换成数学模型并将其实现,这真的是一个有趣的过程。

E201_05_06_矩阵转置

题目要求:输出用户输入的矩阵,输出其对应的转置矩阵。
解题思路:
(1) 输入矩阵的行数和列数。
(2) 输入矩阵的值。
(3) 将矩阵行号和列号互换,输出转置矩阵。
代码

package HomeWork.Part5;

import java.util.Scanner;

public class HomeWork_Part5_05_JuZhengZhuanZhi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入矩阵的行数:");

        int row1 = scan.nextInt();
        System.out.println("请输入矩阵的列数:");
        int row2 = scan.nextInt();
        niZhuan(row1,row2,scan);
    }

    private static void niZhuan(int row1, int row2,Scanner scan) {
        int [][]arrays = new int[10][10];
        //输入数据
        System.out.println("请输入矩阵的值");
        for(int i = 0;i < row1;i++) {
            for (int j = 0; j < row2; j++) {
                arrays[i][j]=scan.nextInt();
            }
        }
        //矩阵逆转
        System.out.println("转置后的矩阵为:");
        for(int i = 0;i < row2;i++) {
            for (int j = 0; j < row1; j++) {
                System.out.print(arrays[j][i]+"\t");
            }
            System.out.println();
        }
    }


}

运行结果:
请输入矩阵的行数:
3
请输入矩阵的列数:
4
请输入矩阵的值
1 2 4 5
3 4 6 7
3 4 6 9
转置后的矩阵为:
1 3 3
2 4 4
4 6 6
5 7 9
心得体会:
矩阵的转置考察到了循环变量的变化,因此灵活运用循环变量很重要。

E201_05_07_杨辉三角

题目要求:输出杨辉三角

解题思路:
(1) 输入杨辉三角形的行数。
(2) 根据杨辉三角形的规律给第一列赋初值0,然后将未知数上一行的左右两个数求和,算出未知数,依次类推直到求出所有数据。
(3) 输出杨辉三角形。
代码:

package HomeWork.Part5;

import java.util.Scanner;

·public class HomeWork_Part5_06YangHuiSanJiao {
    public static void main(String[] args) {
        Scanner  scan = new Scanner(System.in);
        System.out.println("请输入需要打印的杨辉三角形的行数:");
        int n = scan.nextInt();
        int[][] arrays = new int[100][100];

        //给第一列赋值为1
        for(int i=0;i<n;i++){
            arrays[i][0]=1;
        }
        //计算各值
        for(int i=1;i<n;i++){
            for(int j=1;j<=i;j++){
                arrays[i][j]=arrays[i-1][j-1]+arrays[i-1][j];
            }
        }

        //输出杨辉三角形
        System.out.println("结果为:");
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                System.out.print(arrays[i][j]+" ");
            }
            System.out.println();
        }
    }
}

运行结果:
请输入需要打印的杨辉三角形的行数:
6
结果为:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
心得体会:
巧妙利用循环。

因为没有指望,你只能变得更强大。

关注公众号【轻松玩编程】回复关键字“电子书”,“计算机资源”,“Java从入门到进阶”,”JavaScript教程“,“算法”,“Python学习资源”,“人工智能”等即可获取学习资源。

在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值