# JAVA basic programming practice 500 questions

[Procedure 1]
Topic: Classical problem: There is a pair of rabbits. From the third month after birth, a pair of rabbits will be born every month, and the little rabbit will give birth to a pair of rabbits every month after the third month. Do not die, ask what is the number of rabbits in each month?
Program analysis: The law of rabbits is a sequence of 1,1,2,3,5,8, 13, 21.... To do this kind of question, the best way is to find the law, which is the same as
the sequence of high school.  This question has: a [n]=a[n-1]+a[n-1], and the first and second terms are known, and the following values ​​can also be obtained

``public class Programme1 {        public static void main(String[] args) {            System. out.print("请输入你想知道的兔子数量的月 份： ");            Scanner scanner=new Scanner(System.in);            int n=scanner.nextInt();//获取输入的整数            System. out.println("第"+n+"个月 兔子总数为"+fun(n));            scanner.close();        }        //求得所需月 份的兔子的数量， 返回值为兔子的数量        private static int fun(int n){            if(n==1 | | n==2)            return 1;else            return fun(n-1)+fun(n-2);        }    }``

### More 5000 Java interview questions to share : https://blog.csdn.net/qq_17010193/article/details/116722956 ( click on the data download page! )

[Program 2]
Topic: Determine how many prime numbers are between 101-200, and output all prime numbers.
Program analysis:
* A prime number is: a number that can only be divisible by 1 or itself, such as: 3, 5, 7, 11, 131...
*Method of judging prime numbers: Divide a number from 2 to sqrt (this number),
*
Practically, this number can be divided by 2 to the number less than 1 itself, but the calculation time increases. *If it can be divided evenly, it means that the number is not a prime number, otherwise it is a prime number.

``public class Programme2 {        public static void main(String[] args) {            int sum=0;            for (int i = 100; i < 200; i++) {                if (IsRightNum(i)) { //判断这个数是不是素数                    System. out.print(i+" ");                    sum++;                    if (sum%10==0) { //十个一行                        System. out.println();                    }                }            }            System.out.println("素数的整数： "+sum);        }        //判断这个数是不是素数的具体代码        private static boolean IsRightNum(int i) {            for (int j = 2; j < Math.sqrt(i); j++) {                if (i%j==0) { //如果能整除， 就说明不是素数， 可以马上中断， 继续对                    下一个数判断                    return false;                }            }            return true;        }    }``

[Program 3]
Topic: Print out all the "daffodil numbers". The so-called "daffodil numbers" refers to a three-digit number whose cube sum is equal to the number itself.

For example: 153 is a "daffodil number", because 153 = 1 cube + 5 cube + 3 cube.
Program analysis: Use the for loop to control 100-999 numbers, and each number is decomposed into units, tens, and hundreds.

``public class Programme3 {        public static void main(String[] args) {            int sum=0;//水仙花的总数            for (int i = 100; i < 1000; i++) {                int bite=i%10; //求得个位                int ten=i/10%10; //求得十位                int hundred=i/100;//求得百位//如果符合水仙花条件的数打印出来                if (i==(bite*bite*bite)+                        (ten*ten*ten)+(hundred*hundred*hundred)) {                    System. out.print(i+" ");                    sum++;                }            }            System. out.println("总共有水仙花个数： "+sum);        }    }``

[Program 4]
Topic: Decompose a positive integer into prime factors. For example: Enter 90 and print out 90=2*3*3*5.
Program analysis: To decompose the prime factors of n, first find the smallest prime number k, and then complete the steps as follows:
(1) If the prime number is exactly equal to n, it means that the process of decomposing prime factors has ended, just print it out. .
(2) If n<>k, but n is divisible by k, print out the value of k and divide n by the quotient of k as a new positive integer n, and repeat the first step.
(3) If n is not divisible by k, use k+1 as the value of k, and repeat the first step.

``public class Programme4 {        public static void main(String[] args) {            System. out.print("请输入一个你要分解的正整数： ");            Scanner scanner=new Scanner(System.in);            int input=scanner.nextInt();//获取输入的数字            System. out.println();            System. out.print(input+"=");            for (int i = 2; i < input+1; i++) {                while(input%i==0&&input! =i) {                    input=input/i;                    System. out.print(i+"*");                }                if(input==i){ //上面的都不能整除， 说明这是一个质数                    System. out.print(i);                    break;                }            }            scanner.close();        }    }``

[Procedure 5]
Question: Use the nesting of conditional operators to complete this question: Students with academic performance >=90 points are represented by A, those with 60-89 points are represented by B, and those with less than 60 points are represented by C.
Program analysis: (a>b) ?a: b This is a basic example of conditional operators.

``public class Programme5 {        public static void main(String[] args) {            System. out.println("请输入你的分数： ");            Scanner scanner=new Scanner(System.in);            int input=scanner.nextInt();//获取输入//等级判断            String belong=input>=90?"A":(input>=60?"B":"c");            System. out.println(input+"分属于： "+belong);            scanner.close();        }    }``

[Program 6]
Topic: Enter two positive integers m and n, and find the greatest common divisor and least common multiple.
Program analysis: Use the elimination method.
*There is a knowledge point to remember, the method of finding the greatest common divisor and the least common multiple
*1, first find the greatest common divisor bigDivisor
*2, you can easily get the least common multiple multiple=input1*input2/bigDIvisor
*here is the most important It is to find the greatest common divisor: The method is as follows
*(1) Use a large number to calculate the remainder of the small number
* (2) Assign the small number to the large number, and assign the result obtained by the remainder to the small number,
*( 3) Loop the operation of the previous step until the result of the remainder is zero
* (4) The remainder of the previous step is the greatest common divisor we want. If you don’t believe it, you can try it

`` public class Programme6 {        public static void main(String[] args) {            int bigDivisor=0;//定义最大公约数            int multiple=0;//定义最小公倍数            System. out.println("请输入两个整数： ");            Scanner scanner = new Scanner(System.in);            int input1 = scanner.nextInt();// 获取第一个数            int input2 = scanner.nextInt();// 获取第二个数            multiple=input1*input2;//这个值保存， 求公约数后， 方便求得最小公倍数            int temp =1;// 交换用的中间数            if (input2 > input1) {//确保第一个数不小于第二个数                temp=input1;                input1=input2;                input2=temp;            }            while (temp! =0) { //求余结果不等于零， 就一直循环                temp=input1%input2;//求余结果                input1=input2;//大的数已经没用了， 用小的数替代                input2=temp;//把求余的结果赋值给小的数            }            bigDivisor=input1;//最后一次求余结果为零时， 被求余的数            multiple=multiple/bigDivisor;            System. out.println("最大公约数是： "+bigDivisor );            System. out.println("最小公倍数是： "+multiple);            scanner.close();        }    }``

【Procedure 7】
Title: Enter a line of characters and count the number of English letters, spaces, numbers and other characters.
Program analysis:
the required knowledge points here:
1. Get a line of string, nextLine()
2. Assign each character of the string to a value
3. Compare each value in the range of the ASK code, you can determine it Symbol category
4. Char character ASK code range
(1) Numbers 0 to 9: 48~57
(2) Letters A to Z: 65 to 90 a to z: 97 to 122
(3) Space is 32

``public class Programme7 {        public static void main(String[] args) {            int num=0;//数字的个数            int letter=0;//字母的个数            int space=0;//空格的个数            int others=0;//其他的个数            System. out.println("请输入一串字符： ");            Scanner scanner=new Scanner(System.in);            String string=scanner.nextLine();//获取一行字符串//把字符串里面的值赋值给一个字符型数组            char[] arr=string.toCharArray();//遍历字符串里面的所有值            for (int i = 0; i < arr.length; i++) {                if (arr[i]>=48&&arr[i]<=57) {//字符是数字                    num++;                } else if((arr[i]>=65&&arr[i]<=90)| | (arr[i]>=97&&arr[i]<=122)) {                    letter++;                }else if (arr[i]==32) {                    space++;                }else {                    others++;                }            }            System. out.println("数字： "+num+"个， 字母： "+letter+"个， 空格："+space+"个， 其他： "+others+"个");            scanner.close();        }    }``

[Program 8]
Topic: Find the value of s=a+aa+aaa+aaaa+aa... a, where a is a number. E.g. 2 + 22 + 222 + 2222 + 22222 (in this case a total of five
numbers together), a few numbers together with a keyboard. The form of the output result is as follows: 2+22+222=246;
Program analysis: The key is to calculate the value of each item.
For example, the number obtained is: a, the number of items appearing is: n
To sum up, we can get the following rules:
1. The first item has an a, and the last item has n a
2. The difference between the first and second items is 2* 10. The difference between the 2nd and the third term is 2*100, and the difference between the kth and k+1 term is 2* (10 to the k power)

``public class Programme8 {        public static void main(String[] args) {            int n=0,a=0;            Scanner scanner=new Scanner(System.in);            System. out.println("请输入a的值: ");            a=scanner. nextInt();            System. out.println("请输入n的值： ");            n=scanner. nextInt();            int[] arr=new int[n];//创建数组长度为输入的项数            int i=1;//while循环初始化的值            arr[0]=a;//数组的第一个值为3//把每一项的值赋值给数组里面的数            while (i<n) {                a*=10;                arr[i]=a+arr[i-1];                i++;            }//求和            int sum=0;            for(int s: arr){                sum+=s;//累加求和                if (s==arr[n-1]) {                    System. out.print(s);                    break;//最后一次只输出结果                }                System. out.print(s+"+");//前面的值输出结果， 后面还要加一个加号            }            System. out.println("="+sum);//完美结束        }    }``

[Program 9]
Topic: If a number is exactly equal to the sum of its factors, this number is called "complete number". For example, 6=1+2+3. Program to find
all the numbers within 1000 .
The method of judging the number:
* Use the for loop to determine whether the sum of all factors is equal to the input value, and output if they are equal.
* The method of finding the factor:
* (1) Two nested loops, and use i%j==0, Regarding the value range of i and j: i is traversed one by one from 1 to 1000, and j only needs to be no greater than i/2+1. For example: 48, the largest factor is 24, 99 the largest factor is 33, the factor will not be more than half of its own number
* (2) j is the factor we are looking for, add all j to get the sum of the factors
* (3) The sum of factors already contains 1 because 1 is saved the first time

``public class Programme9 {        public static void main(String[] args) {            System. out.println("1000以内的因数有： ");            for (int i = 1; i <=1000; i++) {                int sum=0;//所有因数的总和                for (int j = 1; j < i/2+1; j++) {                    if (i%j==0) { //判断范围内的所有j相加， 就是因数总和                        sum=sum+j;                        if (i==sum) {                            System. out.print(i+" ");                        }                    }                }            }        }    }``

[Procedure 10]
Title: A ball falls freely from a height of h meters, and bounces back to half of its original height each time
it hits the ground. When it falls again, how many meters does it pass in the nth landing? How high is the nth rebound?
Program analysis: Rebound height: (1/2) n-th power *h
* Distance traveled: This can be summed up: The first landing pass: h, the second landing pass: h+(h/2) *2 ,
* The third landing pass: h+(h/2) *2+(h/2/2) *2
* Then the nth landing pass: h+(h/2) *2+(h/2/2) *2 +... +h/(2 to the power of n-1) *2

``public class Programme10 {        public static void main(String[] args) {            System. out.println("请输入小球下落的高度和落地的次数： ");            Scanner scanner=new Scanner(System.in);            float h=scanner.nextFloat();            float n=scanner.nextFloat();//float h=100,n=3;            float sum=h;//经过的路径总和            h/=2;//第一次下落是在最高点， sum中不会有两倍的h， 所以写在外面， 循环从第            二次开始            for (int i = 2; i <= n; i++) {//经过的距离的总和                sum+=h*2;//第N次反弹的高度为                h /=2;            }            System. out.println("在"+100+"米， 经过"+n+"次后， 能反弹： "+h+"米， 经过的距离： "+sum);            scanner.close();        }    }``

[Procedure 11]
Question: There are 1, 2, 3, and 4 numbers. How many different three-digit numbers can be formed without repeated numbers? How many are they?
Program analysis: The numbers that can be filled in the hundreds, tens, and ones digits are all 1, 2, 3, and 4. Here we need to use 3 for loops
to judge whether the conditions are met with if, print out the numbers that meet the conditions, and calculate the sum of the numbers

``public class Programme11 {        public static void main(String[] args) {            int sum=0;            for (int bite = 1; bite < 5; bite++) {                for (int ten = 1; ten < 5; ten++) {                    for (int hundred = 1; hundred < 5; hundred++) {                        if (bite! =ten&&bite! =hundred&&ten! =hundred) {//符合条件                            的数字                            System. out.print((hundred*100+ten*10+bite)+" ");                            sum++;//计算个数                            if (sum%10==0) { //十个一行                                System. out.println();                            }                        }                    }                }            }            System. out.println("\n总共有： "+sum+"个这样的数");        }    }``

[Procedure 12]
Topic: The bonus issued by the enterprise is based on the profit commission. When the profit (I) is less than or equal to 100,000 yuan, the bonus can be increased by 10%; when the profit is higher than 100,000 yuan, and
when it is less than 200,000 yuan, the portion of less than 100,000 yuan will be a 10% commission, and more than 100,000 yuan Cocoa commission is 7.5%; between 200,000 and 400,000
, the part higher than 200,000 yuan can be commissioned 5%; between 400,000 and 600,000, the part higher than 400,000 yuan can be commissioned 3%; 600000-1000000
when between, greater than 60 million portion may commission 1.5%, more than 100 million, more than one million yuan portion 1% commission, from the keyboard input
the month Profit I, what is the total number of bonuses that should be paid?
Program analysis: Please use the number axis to divide and locate. Note that the bonus must be defined as a growth integer when defining it.
More than 100,000 200,000 400,000 600,000 1 million 10*0.1 10*0.075 20*0.05 20*0.03 40*0.015 0.01 1 1.75 2.75 3.35 3.95

``public class Programme12 {        public static void main(String[] args) {            System. out.println("请输入你创造的利润(单位： 万元): ");            Scanner scanner=new Scanner(System.in);            while (! scanner.hasNextDouble()) {                System. out.println("请输入金额数字： ");                scanner.next();            }            double profit=scanner.nextDouble();            double bonus=0;            if (profit<=10) {                bonus=profit*0.1;            }else if(profit<=20){                bonus=(profit-10)*0. 075+1;            }else if (profit<=40 ) {                bonus=(profit-10)*0. 05+1.75;            }else if (profit<=60 ) {                bonus=(profit-10)*0. 03+2.75;            }else if (profit<=100 ) {                bonus=(profit-10)*0. 015+3.35;            }else {                bonus=(profit-100)*0.01+3.95;            }            System. out.println(profit+"万元利润， 可以获得： "+bonus+"万元");            scanner.close();        }    }``

[Procedure 13]
Topic: An integer, after adding 100 to it is a perfect square number, and adding 168 to it is another perfect square number. What is the number?
Program analysis: judge within 100,000,
use for loop to judge: first add 100 to the number and then square, then add 268 to the number and then square, if the result of square root is squared and then sum i +100, i+268 are equal, which is the result.

``public class Prog13{        public class Programme13 {            public static void main(String[] args) {                for (int i = 0; i <10000; i++) {                    int num1=(int)Math.sqrt(i+100);//开方， 值已固定了                    int num2=(int)Math.sqrt(i+268);                    if ((num1*num1==(i+100))&&(num2*num2==(i+268))) {//符合条件的打                        印出来                        System. out.println(i+" ");                    }                }            }        }``

[Procedure 14]
Topic: Enter a certain day, certain month, and certain day. How many days do you judge that day is the year?
Program analysis: Take March 5th as an example. You should add up the previous two months first, and then add 5 days, which is the day of the year. In special circumstances, you need to consider adding an extra day when the input month is greater than 3 in a leap year. .
Leap year conditions: year divided by 400 can be divided evenly, or year divided by 4 can be divided evenly, but cannot be a multiple of 100

``public class Programme14 {        public static void main(String[] args) {            System. out.println("请输入年月 日（用空格隔开） ： ");            Scanner scanner=new Scanner(System.in);            int year=scanner. nextInt();//获取年份            int month=scanner.nextInt();//获取月 份            int day=scanner.nextInt();//获取天数            int sum=0;//天数总和//创建一个包含月 份天数的数组， 先按小年计算， 如果是闰年， 并且在三月 以后再加1            int[] arr={31,28,31, 30,31,30,31,31,30,31,30,31};            sum=day;//输入的天数肯定是要加的            for (int i = 1; i < month; i++) { //加上包含的月 份天数                sum+=arr[i];            }//最后判断是否是闰年， 如果是的话再加1， 否则sum就是结果了,条件比较长， 我就抽出来写了            boolean isRight=(((year%4==0)&&(year%100! =0))| | (year%400==0))&&(month>2);            if (isRight) {                sum+=1;            }            System. out.println(year+"年"+month+"月 "+day+"日， 是这年的第"+sum+"天");        }    }``

[Program 15]
Title: Input three integers x, y, z, please output these three numbers from small to large.
Program analysis: We find a way to put the smallest number on x, first compare x and y, if x>y, exchange the values ​​of x and y, and then compare x and z, if x>z Then exchange the values ​​of x and z so that x can be minimized.
The last two numbers also make z>y.

``public class Programme15 {        public static void main(String[] args) {            System. out.println("三个整数： ");            Scanner scanner=new Scanner(System.in);            int num1=scanner. nextInt();//获取整数            int num2=scanner. nextInt();            int num3=scanner. nextInt();            int temp=0;//最为一个交换数            if (num1>num2) {//保证num2>num1                temp=num1;                num1=num2;                num2=temp;            }            if (num1>num3) {//保证num3>num1                temp=num1;                num1=num3;                num3=temp;            }            if (num2>num3) {//保证num3>num2                temp=num2;                num2=num3;                num3=temp;            }            System. out.println("这三个数从小到大排列： "+num1+" "+num2+" "+num3);            scanner.close();        }    }​``

[Program 16]
Title: Output 9*9 formulas.
Program analysis: Considering the rows and columns, there are 9 rows and 9 columns, i control row, jC column.
Expression: i+"*"+j+"="+i*j, where two for loops are used to control output and line feed

``public class Programme16 {            public static void main(String[] args) {                for (int i = 1; i <= 9; i++) {                    for (int j = 1; j <=i ; j++) {                        System. out.print(i+"*"+j+"="+i*j+" ");//输出结果                    }                    System. out.println();//换行                }            }        }``

[Procedure 17]
Topic: Monkey eating peach problem: The monkey plucked a few peaches on the first day, and ate half of it immediately, and was not addicted to it. He ate one more and ate half of the remaining peaches the next morning. Eat one more. After that, every morning I ate half and one of the remaining half of the previous day. When I wanted to eat again in the morning of the 10th day, I saw that there was only one peach left. Ask for the total number of picks on the first day.
Program analysis: Adopt the method of reverse thinking, infer from the back to the front.
The number of days is 1 2 3 4 5. . . 10
peaches number 1 4 10 22 46?
* So the calculation method of the number of peaches: the number of peaches the day before *2+2

``public class Programme17 {            public static void main(String[] args) {                int sum=1;//第一天桃子的数量                for (int i =2; i <=10; i++) {//第二天才开始计算                    sum=sum*2+2;                }                System. out.println("猴子摘的桃子数为： "+sum);            }        }``

[Procedure 18]
Topic: Two table tennis teams compete, each with three players. Team A is composed of a, b, and c, and Team B is composed of x, y, and z. Lots have been drawn to determine the list of matches.
Someone asked the players for the roster of the game. a says he does not compare with x, c says he does not compare with x, z, please program to find the list of the three teams.
Program analysis:? ? ? I feel like I can't start!
* This topic uses the construction method, and the knowledge of arrayList! The answer to the standard answer!
* However, it was solved by me using a for loop.
* The important thing here is to use assignment, and to make full use of the given conditions, and there is a common sense that does not conflict with each other, such as the use of the first if!

``public class Programme18 {        public static void main(String[] args) {            String a = null,b= null,c= null;//甲队成员            String[] racer = {"x","y","z"};//乙队成员            for(int i=0;i<3;i++){                for (int j = 0; j <3; j++) {                    for (int k = 0; k < 3; k++) {                        if ( i! =j&&i! =k&&j! =k) {                            a=racer[i]; b=racer[j];c=racer[k];                            if                            (! a.equals("x")&&! c.equals("z")&&! c.equals("x")) {                                if                                (a.equals(racer[i])&&b.equals(racer[j])&&c.equals(racer[k])) {                                    System.out.println("a的对手是： "+                                            racer[i]+"， b的对手是： "+racer[j] +"， c的对手是： "+racer[k] );                                }                            }                        }                    }                }            }        }    }``

[Program 19]
Topic: Print out the following pattern (diamond)
*
***
******
********
******
***
*
Program analysis: First divide the graph into two parts To look at it, the first four lines have a rule, and the last three lines have a rule.
Using a double for loop, the first level controls the rows and the second level controls the columns.
Analyze the pattern of the first part in detail:
1. This is an isosceles triangle. The number of prints of * is 1, 3, 5, 7 and the number sequence is equal to
2. It should also be noted that the number of prints of space 3, 2, 1, 0 gradually decreases
* The second part is available in the same way!

``public class Programme19 {            public static void main(String[] args) {//打印上面部分：                int n=4;                for (int i = 0; i < n; i++) { //控制行                    for (int k =3-i; k > 0 ; k--) { //控制*前面空格的输出                        System. out.print(" ");                    }//输出符号， 但不要换行                    for (int j = 0; j <=2*i; j++) { //控制列                        System. out.print("*");                    }//输出完符号马上换行                    System. out.println();                }//打印下面部分                n=3;                for (int i = n; i > 0; i--) { //控制行                    for (int k =3-i+1; k > 0 ; k--) { //控制*前面空格的输出,第一行要                        加空格                        System. out.print(" ");                    }//输出符号， 但不要换行                    for (int j = 0; j <=2*i-2; j++) { //控制列                        System. out.print("*");                    }//输出完符号马上换行                    System. out.println();                }            }        }``

[Program 20]
Problem: There is a sequence of scores: 2/1, 3/2, 5/3, 8/5, 13/8, 21/13... Calculate the sum of the first 20 items of this sequence.
Program analysis: Please grasp the changing law of the numerator and denominator. Fraction:
1. The first item is 2/1
2. The sum of the numerator and denominator of the previous item is the numerator of the next item
*3, and the numerator of the previous item is the denominator of the next item.

``public class Programme20 {            public static void main(String[] args) {                float up=2;//分子                float down=1;//分母                float fraction=up/down;//分数                float temp=0;//交换数                float sum=0;//总和                for (int i = 0; i <20; i++) { //前面20项                    sum+=fraction; //分数相加                    temp=up+down; //先把分子分母的和赋值给一个交换数， 不能马上赋值给                    分子， 因为分子的值下面还有用                            down=up;//把分子的值赋值给下一下的分母                    up=temp;//把交换数的值赋值给下一项分子                    fraction=up/down;//下一项的分数值                }                System. out.println(""+sum);            }        }``

[Program 21]
Topic: Find
the sum of 1+2!+3!+... +20! and program analysis: This program just turns the accumulation into the accumulation multiplication.
1. First find the value of the factorial of the term
* 2. Find by accumulation

`` public class Programme21 {            public static void main(String[] args) {                int sum=0;//总和                for (int i = 1; i <= 20; i++) {                    sum+=factorial(i);//累加                }                System. out.println(""+sum);            }            //求阶乘的实现            private static int factorial(int i) {//求阶乘                int mult=1;                for (int j=1 ; j <= i;j++) {                    mult*=j;                }                return mult;//返回阶乘结果            }        }``

[Program 22]
Topic: Use recursion to find 5!.
Program analysis: Recursive formula: f(n) =n*f(n-1)
call the method continuously, until the minimum value is determined

``public class Programme22 {            public static void main(String[] args) {                System. out.println(Fact(5));//输出结果值            }            //递归方法求阶乘的具体代码            private static int Fact(int i) {                if (i==1) {//1的阶乘为1                    return 1;                }                return i*Fact(i-1);//不断回调            }        }``

[Procedure 23]
Topic: 5 people are sitting together, and ask how old is the fifth person? He said he was 2 years older than the fourth person. Asked the age of the fourth person, he said that he was 2 years older than the third person. Ask the third person and say that he is two years older than the second person. Ask the second person and say that he is two years older than the first person. Finally, I asked the first person, he said he was 10 years old. How old is the fifth person?
Program analysis: Using the method of recursion, recursion is divided into two stages: backtracking and recursive. If you want to know the age of the fifth person, you need to know the age of the fourth person, and so on, push to the first person (10 years old), and then push back.

``public class Programme23 {            public static void main(String[] args) {                System. out.println("第五个人的岁数： "+GetAge(5,2));            }            //递归方法求第五个人的岁数            private static int GetAge(int num, int ageMore) {                if (num==1) {//第一个人岁数为10                    return 10;                }                return GetAge(num-1, ageMore)+ageMore;//每上一个人岁数加2            }        }``

【Program 24】
Title: Give a positive integer with no more than 5 digits. Requirements: 1. Find the number of digits in it. 2. Print out the digits in reverse order.
Program analysis: Create an array of five-digit numbers, and assign the value of each digit of the input number to the array
* Get the input number from the ones place, and after each acquisition, /10, so that you can get the tens place separately Hundreds and Thousands
* Printing out the numbers is what the title asks for

``public class Programme24 {            public static void main(String[] args) {                System. out.println("请输入一个不大于5位数的数字： ");                Scanner scanner=new Scanner(System.in);                int num=scanner.nextInt();//获取输入                int[] arr=new int[5];//创建5位数的数组                int i=0;//循环取位                do{                    arr[i]=num%10;                    num=num/10;                    i++;                }while (num! =0);//只剩下一位时， 说明获取完毕， 跳出循环                System. out.println("输入数字串的是"+i+"位数的");                System. out.println("逆序输出： ");//打印出数组                for (int j = 0; j < i;j++) {                    System. out.print(arr[j]+" ");                }                scanner.close();            }        }``

[Procedure 25]
Title: A 5-digit number, judge whether it is a palindrome or not. That is, 12321 is the number of palindrome, the ones place is the same as the ten thousand place, and the ten place is the same as the thousand place.
This procedure is similar to the previous one.
* Create a five-digit array
* Take the digits one by one
* After the group, it is enough to judge the conditions

``public class Programme25 {            public static void main(String[] args) {                System. out.println("请输入一个五位数： ");                Scanner scanner=new Scanner(System.in);                int input=scanner.nextInt();//获取输入的数字                int arr[]=new int[5];//创建一个大小为5 的数组                int i=4;                do {//逐次取位                    arr[i]=input%10;                    input/=10;                    i--;                } while (i>=0);//这里的结束条件写input！ =0也是可以的//System.out.println(Arrays.toString(arr));                if (arr[0]==arr[4]&&arr[1]==arr[3]) {                    System. out.println("输入的数是回文数字！ ");                }else {                    System. out.println("输入的数不是回文数字！ ");                }                scanner.close();            }        }``

【Procedure 26】
Title: Please enter the first letter of the day of the week to determine the day of the week. If the first letter is the same, continue to determine the second letter.
Program analysis: It is better to use case statements. If the first letter is the same, then use case statements or if statements to determine the second letter.

``public class Programme26 {            public static void main(String[] args) {                System. out.println("请输入第一个英文字母： ");                Scanner scanner=new Scanner(System.in);                String input=scanner.next();                String input2="";                switch (input) {                    case "m":                        System. out.println("Monday");                        break;                    case "t":                        System. out.println("请输入第二个字母： ");                        input2=scanner.next();                        if (input2.equals("u")) {                            System. out.println("Tuesday");                        }else if (input2. equals("h")) {                            System. out.println("Thursday");                        } else {                            System. out.println("你输入的字母有误！ ");                        }                        break;                    case "w":                        System. out.println("Wednesday");                        break;                    case "f":                        System. out.println("Friday");                        break;                    case "s":                        System. out.println("请输入第二个字母： ");                        input2=scanner.next();                        if (input2.equals("u")) {                            System. out.println("Sunday");                        }else if (input2. equals("a")) {                            System. out.println("Saturday");                        } else {                            System. out.println("你输入的字母有误！ ");                        }                        break;                    default:                        System. out.println("你输入的字母不正确！ ");                        break;                }                scanner.close();            }        }``

[Procedure 27]
Topic: How to find prime numbers within 100:
Judgment method for prime numbers: If this number is divided by 2 to a number that is 1 less than itself, it is not evenly divisible, then this number is a prime number!

``public class Programme27 {            public static void main(String[] args) {                int sum=0;                for (int i = 2; i < 100; i++) {                    if (IsRight(i)) {//如果是素数就打印出来                        System. out.print(i+" ");                        sum++;                        if (sum%10==0) {//十个一行                            System. out.println();                        }                    }                }                System. out.println("\n共有素数： "+sum+"个");            }            //判断该数是不是素数            private static boolean IsRight(int i) {                for (int j = 2; j < Math.sqrt(i); j++) {                    if (i%j==0) {                        return false;//不是素数的话， 马上返回false                    }                }                return true;            }        }``

[Program 28]
Topic: Sort 10 numbers.
Program analysis: You can use the selection method, that is, from the last 9 comparisons, choose the smallest one to exchange with the first element, and analogize next time, that is, use the second element Compare with the last 8 and exchange.
The last printed array is the array arranged from small to large

``public class Programme28 {            public static void main(String[] args) {                System. out.println("请输入10个数（用空格隔开） ： ");                int[] arr=new int[10];                Scanner scanner=new Scanner(System.in);                for (int i = 0; i < 10; i++) {                    arr[i]=scanner.nextInt();                }//对数组进行排序                int temp=0;//交换数                for (int i = 0; i < 9; i++) {                    for (int j = i+1; j < arr.length; j++) {                        if (arr[i]>arr[j]) { //如果第一个数比后面的数大就交换                            temp=arr[i];                            arr[i]=arr[j];                            arr[j]=temp;                        }                    }                }                System. out.println("排列后的数组： "+Arrays.toString(arr));//Arrays                类的包装方法！            }        }``

[Program 29]
Topic: Find the sum of diagonal elements of a 3*3 matrix.
Program analysis: Use double for loop control to input a two-dimensional array, and then accumulate a[i][i] and output.

``public class Programme29 {        public static void main(String[] args) {            System. out.println("请输入九个数字： ");            Scanner scanner=new Scanner(System.in);            int[][] arr=new int[3][3];//获取矩阵数字            for (int i = 0; i <3; i++) {                for (int j = 0; j < 3; j++) {                    arr[i][j]=scanner.nextInt();                }            }            System. out.println("第一条对角线之和："+(arr[0][0]+arr[1][1]+arr[2][2]));            System. out.println("第二条对角线之和："+(arr[0][2]+arr[1][1]+arr[2][0]));            scanner.close();        }    }``

[Program 30]
Topic: There is an array that has been sorted. Now enter a number and require it to be inserted into the array according to the original rules.
Program analysis: First, judge whether this number is greater than the last number, and then consider the case of inserting the middle number. After inserting the number after this element, move one position backward in turn.
1. Create two arrays. If the inserted number is larger than the last one of the array, then the inserted number can be placed at the end of the new array
* 2. If the inserted number is not the largest, then compare with the previous number of the array one by one to compare If the number of insertions is large, break. For example, if the i-th meets the condition, then arrB[i-1]=arrA[i-1], arrB[i]=num, arrB[i+1]=arrA[i]

``public static void main(String[] args) {        System. out. println("请输入一个数字： ") ;        Scanner scanner=new Scanner(System. in) ;        int num=scanner. nextInt() ;        int[] arrA={3, 5, 15, 36, 84, 99};        int[] arrB=new int[arrA. length+1] ;        if (num>arrA[arrA. length-1] ) {            for (int i = 0; i < arrA. length; i++) {                arrB[i] =arrA[i] ;            }            arrB[arrB. length-1] =num;        }else {            for (int i = 0; i < arrA. length; i++) {                if (num<arrA[i] ) {                    for (int j = 0; j < i; j++) {                        arrB[j] =arrA[j] ;                    }                    arrB[i] =num;                    for (int j = i; j < arrA. length; j++) {                        arrB[j+1] =arrA[j] ;                    }                    break; //这个很重要                }            }        }        System. out. println("插入一个数后的数组为："+Arrays. toString(arrB) ) ;        scanner. close() ;    }}``

【Program 31】
Title: Output an array in reverse order.
Program analysis: use the first and last exchange.

``public static void main(String[] args) {        int[] arrA={1, 3, 44, 22, 77, 99};        int[] arrB=new int[arrA. length] ;        int j=arrA. length;        for (int i = 0; i < arrA. length; i++) {            arrB[i] =arrA[j-1] ;            j--;        }        System. out. println("数组 A 逆序输出为： "+Arrays. toString(arrB) ) ;    }}``

【Program 32】
Title: Take an integer a from 4 to 7 digits from the right end.
Program analysis: Think about it like this:
see the detailed explanation below

``public static void main(String[] args) {        System. out. println("输入一个整数： ") ;        Scanner scanner=new Scanner(System. in) ;        long num=scanner. nextLong() ; //获取输入        String str=Long. toString(num) ; //把数字转化为 String 类型的        char[] ch=str. toCharArray() ; //把 String 类型的字符， 转化为 char 类型，每一个数字赋值到字符型数组中        int n=ch. length; //字符型数组的长度        System. out. println("该整数从右端开始的 4-7 位为："+ch[n-7] +ch[n-6] +ch[n-5] +ch[n-4] ) ; //输出结果        scanner. close() ; //关闭输入流    }``

[Program 33]
Title: Print out Yang Hui's triangle (10 lines are required to be printed as shown below)
Program analysis:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1. Use of two-dimensional array
2. The number in the first column is 1.
3. Use two for loops, i control the row and j control the column;
starting from the second row and second column arr[i] [j] = arr[i-1] [j -1] +arr[i-1] [j]

`` public static void main(String[] args) {        int[] [] arr=new int[10] [10] ;        for (int i = 0; i < arr. length; i++) {             arr[i] [0] =1; //控制第一列的数等于 1         }        for (int i = 1; i < arr. length; i++) {            for (int j=1; j < arr. length; j++) {                arr[i] [j] =arr[i-1] [j-1] +arr[i-1] [j] ; //赋值            }        }//打印结果        for (int i = 0; i < arr. length ; i++) {            for (int k = arr. length-i; k>0; k--) {//空格的控制， 为了好看                System. out. print(" ") ;            }            for (int j = 0; j < arr. length; j++) {//打印出数组的数字                if (arr[i] [j] ! =0) {//把把没赋值的零去掉                    System. out. print(arr[i] [j] +" ") ;                }            }            System. out. println() ; //控制换行        }    }``

【Program 34】
Title: Input 3 numbers a, b, c, and output them in order of size.
Program analysis: Use pointer method.

``public static void main(String[] args) {        System. out. println("请输入三个数： ") ;        Scanner scanner = new Scanner(System. in) ;        int num1 = scanner. nextInt() ; //获取输入的数        int num2 = scanner. nextInt() ;        int num3 = scanner. nextInt() ;        scanner. close() ;        int temp = 0;        if (num1 > num2) {//确保 num2>num1            temp = num1;            num1 = num2;            num2 = temp;        }        if (num1 > num3) {//确保 num3>num1            temp = num1;            num1 = num3;            num3 = temp;        }        if (num2 > num3) {//确保 num3>num2            temp = num2;            num2 = num3;            num3 = temp;        }        System. out. println("这三个数从大到小排列： "+num3+" "+num2+" "+num1 ) ;    }``

[Program 35]
Topic: Input array, exchange the largest element with the first element, exchange the smallest element with the last element, and output the array.
Program analysis: 1. Find the maximum and minimum values ​​of
the array* Find the maximum item of the array and exchange it with the first one,
* Find the minimum item of the value and exchange it with the last item
* There is one point worth noting here: No Find the maximum value and minimum value first, and then perform two consecutive operations to exchange, it will be an error!
* One more thing to note: If the position of the value to be exchanged is not called, the position of the value that I checked on the Internet will be out of order.

``public static void main(String[] args) {        System.out.print("请输入一组数： ");        Scanner scan = new Scanner(System.in).useDelimiter("\\s");        int[] arrA = new int[50];        int m = 0; //数组的长度        while (scan.hasNextInt()) {//不断给数组 A 赋值            arrA[m++] = scan.nextInt();        }        scan.close(); //关闭输入流        int[] arrB = new int[m]; //创建数组 B， 要求刚好适应输入的数字的个数        for (int i = 0; i < m; i++) {            arrB[i] = arrA[i]; //把数组 A 不为零的数值赋值给数组 B        }        int max = 0;        int min = arrB[1]; //定义数组的最小值        for (int i = 0; i < arrB.length; i++) {            if (arrB[i] > max) {//求数组的最大值                max = arrB[i];            }            if (arrB[i] <= min) {//求数组的最小值                min = arrB[i];            }        }        int max_i = 0; //定义数组最大值的下标        int min_i = 0; //定义数组最小值的下标        for (int i = 0; i < arrB.length; i++) {            if (max == arrB[i]) {                max_i = i;            }        }        int temp = 0; //定义一个交换值,把最大的值和第一个值交换        temp = arrB[0];        arrB[0] = arrB[max_i];        arrB[max_i] = temp;//求数组最小值的下标        for (int i = 0; i < arrB.length; i++) {            if (min == arrB[i]) {                min_i = i;            }//把最小的值和最后一个值交换            temp = arrB[arrB.length - 1];            arrB[arrB.length - 1] = arrB[min_i];            arrB[min_i] = temp;            System.out.println(Arrays.toString(arrB));            scan.close();        }    }``

[Procedure 36]
Subject: There are n integers, make the previous numbers move backward m positions, and finally m numbers become the first m numbers.
Program analysis: Create a sum on the basis of the original array The original array of the same size
* The original array is divided into two parts according to the number of bits to be moved and assigned to the new array respectively

``public static void main(String[] args) {        System. out. println("输入一个十个数的组数： ") ;        Scanner scanner=new Scanner(System. in) ;        int n=10;        int[] arrA=new int[n] ;        for (int i = 0; i < n; i++) {            arrA[i] =scanner. nextInt() ; //获取十个数组        }        System. out. println("没移动前的数组： "+Arrays. toString(arrA) ) ;        System. out. println("请输入要往后移动的个数： ") ;        int m=scanner. nextInt() ; //获取输入往后退的个数        m%=n; //十个相当于循环        int[] arrB=new int[n] ; //创建和数组 A 大小一样的数组 B        int k=m; //创建一个可变的变量        for (int i = m; i < arrA. length; i++) {            arrB[i] =arrA[i-m] ;        }        for (int i = 0; i <m; i++) {            arrB[i] =arrA[arrA. length-k] ;            k--;        }        System. out. println("移动后的数组： "+Arrays. toString(arrB) ) ; //输出数组 B        scanner. close() ;    }``

【Procedure 37】
Title: There are n people in a circle and arrange the numbers in order. Start counting from the first person (from 1 to 3). Anyone who reports to 3 exits the circle and asks which person is the original number left at the end.
The key question is how to remove it? : Boolean value is determined, if the selected is assigned to false
* judge from the first person, if he is in the circle, then report the number, and then judge whether the number he reported is 3, if it is, move out of the circle
* then judge The next person, is it at the end, if so, start from scratch
* and keep looping until there is 1 person left at the end

``public static void main(String[] args) {        System. out. print("请输入一个整数： ") ;        Scanner scan = new Scanner(System. in) ;        int n = scan. nextInt() ;        scan. close() ;//定义数组变量标识某人是否还在圈内        boolean[] isIn = new boolean[n] ;        for(int i=0; i<isIn. length; i++) {            isIn[i] = true;        }        int inCount = n; //定义圈内人数        int countNum = 0; //定义报数        int index = 0; //定义索引        while(inCount>1) {//最后一人时退出循环            if(isIn[index] ) {//判断下一个人是否在圈内                countNum++; //在的话报数                if(countNum==3) {//如果这个数等于 3                    isIn[index] = false; //把他定义为出圈                    countNum = 0; //报数清零， 下一个好从 1 开始                    inCount--; //圈内人数减一                }            }            index++; //下一人的位置索引值            if(index==n) {//当索引到最后之后再从头开始                index = 0;            }        }        for(int i=0; i<n; i++) {            if(isIn[i] ) {//最后只有一个符合条件的                System. out. println("留下的是： "+(i+1) ) ;            }        }    }``

[Program 38]
Topic: Write a function to find the length of a character string, enter the character string in the main function, and output its length.

``public static void main(String[] args) {        System. out. println("请输入一串字符： ") ;        Scanner scanner=new Scanner(System. in) ;        String input=scanner. next() ; //获取输入的字符串        System. out. println("你输入的字符串是： "+input) ;        System. out. println("你输入的字符串长度是： "+input. length() ) ;        scanner. close() ;    }``

[Program 39]
Topic: Write a function, when the input n is an even number, call the function to find 1/2+1/4+... +1/n, when the input n is an odd number, call the function 1/1+1/ 3+... +1/n (using pointer function)
program analysis:
1. First judge whether it is odd or even
* 2. The numerator of the fraction is all one, the even denominator starts from 2, the odd denominator starts from 1, and the denominator difference value Both are 2

``import java. util. Scanner;    public static void main(String[] args) {        System. out. println("请输入一个整数： ") ;        Scanner scanner=new Scanner(System. in) ;        int input=scanner. nextInt() ;        double sum=0; //总和        if (input%2==0) {//偶数            for (double i = 2; i <= input; i+=2) {                sum+=1/i;                if (i==input) {                    System. out. print("1/"+(int) i) ; //输出最后一项， 最后用                    break， 中断后面的执行                    break;                }                System. out. print("1/"+(int) i+" + ") ; //输出相加的项后面加个                加号            }        }else {//奇数情况            for (double i = 1; i <= input; i+=2) {                sum+=1/i;                if (i==input) {                    System. out. print("1/"+(int) i) ; //输出最后一项， 最后用                    break， 中断后面的执行                    break;                }                System. out. print("1/"+(int) i+" + ") ; //输出相加的项后面加个                加号            }        }        System. out. println(" = "+sum) ;        scanner. close() ;    }}``

【Program 40】
Title: String sorting.
Speechless! An encapsulation method is used here: boolean b=string1. compareTo(string2);
* The type value of string1 is greater than string2, then b is greater than zero, otherwise less than zero

``public class Prog40{    public static void main(String[] args) {        String[] str = {"abc", "cad", "m", "fa", "f"} ;        for(int i=str. length-1; i>=1; i--) {            for(int j=0; j<=i-1; j++) {                if(str[j]. compareTo(str[j+1]) <0) {                    String temp = str[j];                    str[j] = str[j+1];                    str[j+1] = temp;                }            }        }        for(String subStr: str)            System. out. print(subStr+" ") ;    }}``

[Program 41]
Topic: There is a pile of peaches on the beach, and five monkeys divide it. The first monkey divided the pile of peach certificates into five, and one more was added. The monkey threw the more into the sea and took one. The second monkey divided the remaining peaches into five equally, and added one more. It also threw the extra one into the sea and took away one. The third, fourth, and fifth monkeys did the same. Yes, how many peaches are there on the beach?
Program analysis: There is a question that the title does not say that the fifth monkey got only one peach, but everyone else knows it? ? I don’t understand
this is actually a recursive problem every time *5 +1
this uses reverse thinking
* 5 4 3 2 1
* The number of peaches 6 31 156 781 3906
* The number of monkeys is 1 6 31 156 781

``public class Programme41 {    public static void main(String[] args) {        System. out.println(Peach(1));;    }    private static int Peach(int i) {        if (i==5) {            return 6;        }        return Peach(i+1)*5+1;    }}``

[Program 42]
Title: 809*??=800*??+9*??+1
where ?? represents two digits, the result of 8*?? is two digits, and the result of 9*?? is 3 Digits. Find the two digits represented by ?? and the result after 809*??.
Program analysis: the use of for loop

``public class Programme42 {    public static void main(String[] args) {        String result="";        for (int i = 100; i < 1000; i++) {            if (809*i==800*i+9*i+1) {                result=i+" ";            }        }        if (result.equals("")) {            System. out.println("没有合适的数");        }else {            System. out.println("合适的数有： "+result);        }    }}``

[Program 43]
Topic: Find the odd number that can be formed from 0-7.
*Program analysis: at least 1 digit, and at most 8 digits.
* The first digit cannot be zero, and the last digit cannot be an even number.
* There are four odd numbers from 0 to 7
* Here we use cumulative sum

``public class Programme43 {    public static void main(String[] args) {        int n = 8;//位数        int sum = 0;//总和// 1位数情况        sum += n / 2;// 2位数情况        sum += (n-1)*n / 2;// 3位数情况        sum += (n-1)*n*n / 2;// 4位数情况        sum += (n-1)*n*n*n / 2;// 5位数情况        sum += (n-1)*n*n*n*n / 2;// 6位数情况        sum += (n-1)*n*n*n*n*n / 2;// 7位数情况        sum += (n-1)*n*n*n*n*n*n / 2;// 8位数情况        sum += (n-1)*n*n*n*n*n*n*n / 2;        System. out.println("0到7能组成的奇数个数： "+sum);    }}``

[Program 44]
Topic: An even number can always be expressed as the sum of two prime numbers.
Program analysis: Judging whether two addends are prime numbers
* The judgment of prime numbers has been done a lot before, let's not talk about it!

``import java. util. Scanner;public class Programme44 {    public static void main(String[] args) {        System. out.println("请输入一个偶数： ");        Scanner scanner =new Scanner(System.in);        int input=scanner.nextInt();        while (input%2! =0) {            System. out.println("你输入的不是偶数， 请重新输入： ");            input=scanner.nextInt();        }        for (int i = 2; i < input; i++) {            if (isRightNum(i)&&isRightNum(input-i)) {//两个加数同时是素数                System. out.println(input+"="+i+"+"+(input-i));                break;            }        }        scanner.close();    }    //判断这个数是不是素数的具体方法：    private static boolean isRightNum(int i) {        for (int j = 2; j < Math.sqrt(i+1); j++) {            if (i%j==0) { //确定是素数                return false;            }        }        return true;    }}``

[Program 45]
Topic: Judging that a prime number can be divisible by several 9s.
Program analysis: I don’t know which fool came up with this topic. I can only say rubbish. I don’t know what it wants to ask?
Below is the answer from the Internet, I don’t want to do it!
I guess it is judging how much greater the input number is to the power of 9? And is it related to prime numbers? ?

``import java. util. Scanner;public class Prog45{    public static void main(String[] args) {        System. out. print("请输入一个数： ") ;        Scanner scan = new Scanner(System. in) ;        long l = scan. nextLong() ;        long n = l;        scan. close() ;        int count = 0;        while(n>8) {            n /= 9;            count++;        }        System. out. println(l+"能被"+count+"个 9 整除。 ") ;    }}``

[Program 46]
Topic: Two string concatenation program
program analysis: Check the basic common sense of the program?

``public class Prog46{    public static void main(String[] args) {        String str1 = "lao lee";        String str2 = "牛刀";        String str = str1+str2;        System. out. println(str) ;    }}``

【Program 47】
Title: Read the integer value of 7 numbers (1-50), every time a value is read, the program prints out the number of the value *.
Program analysis: It's just a simple method call!

``import java. util. Scanner;public class Prog47{    public static void main(String[] args) {        System. out. print("请输入 7 个整数(1-50) ： ") ;        Scanner scan = new Scanner(System. in) ;        int n1 = scan. nextInt() ;        int n2 = scan. nextInt() ;        int n3 = scan. nextInt() ;        int n4 = scan. nextInt() ;        int n5 = scan. nextInt() ;        int n6 = scan. nextInt() ;        int n7 = scan. nextInt() ;        scan. close() ;        printStar(n1) ;        printStar(n2) ;        printStar(n3) ;        printStar(n4) ;        printStar(n5) ;        printStar(n6) ;        printStar(n7) ;    }    static void printStar(int m) {        System. out. print(m) ;        for(int i=0; i<m; i++)            System. out. print("*") ;        System. out. println() ;    }}``

[Program 48]
Topic: A company uses public phones to transmit data. The data is a four-digit integer and is encrypted during the transmission. The encryption rules are as follows: Add 5 to each number, and divide the sum by the remainder of 10. Instead of this number, swap the first and fourth digits, and swap the second and third digits.
Program analysis:
1. Take the number of digits, find out the digits of tens, hundreds, and thousands respectively.
2. Replace
as required. 3. Exchange as required.

``public class Programme48 {    public static void main(String[] args) {        int[] num=new int[4];//存放四位数的个十百千位        System. out.println("请输入一个四位数： ");        Scanner scanner=new Scanner(System.in);        int input=scanner.nextInt();        for (int i = 0; i < 4; i++) {//逐次取位， 从个位开始,并按要求替换            num[i]=(input%10+5)%10;            input/=10;        }//按要求替换,交换1、 4位        int temp=0;//交换数        temp=num[0];        num[0]=num[3];        num[3]=temp;//交换2、 3位        temp=num[1];        num[1]=num[2];        num[2]=temp;        System. out.println("加密后的结果： "+num[3]+num[2]+num[1]+num[0]);        scanner.close();    }}``

[Program 49]
Topic: Calculate the number of occurrences of substrings in a string
Program analysis: Convert each element of a string of string type to the value of each array in the char array string. toCharArray()
* Determine the char array Whether there are spaces, add 1 to the number of substrings if they exist

``public class Programme49 {    public static void main(String[] args) {        String string="aa bb df 23 d 3 df32 d";        int num=1;//子字符的数量        char[] c=string.toCharArray();        for (int i = 0; i < c.length; i++) {            if (c[i]==' ' ) {                num++;            }        }        System. out.println(string+"有子字符串： "+ num +" 个");    }}``

[Program 50]
Topic: There are five students, each student has scores of 3 courses, input the above data (including student ID, name, and scores of three courses) from the keyboard, calculate the average score, and combine the original data with The calculated average score is stored in the disk file "stud".
The last question involves file storage, input and output streams

``import java. io. *;public class Prog50{    //定义学生模型    String[] number = new String[5];    String[] name = new String[5];    float[][] grade = new float[5][3];    float[] sum = new float[5];    public static void main(String[] args) throws Exception{        Prog50 stud = new Prog50() ;        stud. input() ;        stud. output() ;    }    //输入学号、 姓名、 成绩    void input() throws IOException{        BufferedReader br = new BufferedReader(new InputStreamReader(System. in) ) ;//录入状态标识        boolean isRecord = true;        while(isRecord) {            try{                for(int i=0; i<5; i++) {                    System. out. print("请输入学号： ") ;                    number[i] = br. readLine() ;                    System. out. print("请输入姓名： ") ;                    name[i] = br. readLine() ;                    for(int j=0; j<3; j++) {                        System. out. print("请输入第"+(j+1) +"门课成绩： ") ;                        grade[i][j] = Integer. parseInt(br. readLine() ) ;                    }                    System. out. println() ;                    sum[i] = grade[i][0]+grade[i][1]+grade[i][2];                }                isRecord = false;            } catch(NumberFormatException e) {                System. out. println("请输入一个数字！ ") ;            }        }    }    //输出文件    void output() throws IOException{        FileWriter fw = new FileWriter("E: //java50//stud. txt") ;        BufferedWriter bw = new BufferedWriter(fw) ;        bw. write("No. "+"Name "+"grade1 "+"grade2 "+"grade3 "+"average") ;        bw. newLine() ;        for(int i=0; i<5; i++) {            bw. write(number[i]) ;            bw. write(" "+name[i]) ;            for(int j=0; j<3; j++)                bw. write(" "+grade[i][j]) ;            bw. write(" "+(sum[i]/5) ) ;            bw. newLine() ;        }        bw. close() ;    }}``