Java编程练习题

2019年8月8日开始编写,等50题做完了希望不会太晚。

本篇纯属本人的练手作业,本人也不是什么大神,仅供参考。


题目1

package SU;

/**

* @ClassName WayRabbit

* @Description 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

* @Author TumTam

* @Date 2019/8/8 14:12

* @Version 1.0

**/

public class WayRabbit {

public int WayRabbit(int m){

if(m==1|m==2){

return 1;

        }else{

return WayRabbit(m-1)+WayRabbit(m-2);

        }

}

}


package SU;

import java.util.Scanner;

public class WayRabbitTest {

public static void main(String[] args) {

System.out.println("你想知道第几个月的兔子数?");

        Scanner months =new Scanner(System.in);

        int m = months.nextInt();

        months.close();

        WayRabbit wr =new WayRabbit();

        int num =  wr.WayRabbit(m);

        System.out.println("第"+m+"个月的兔子有:"+num+"对");

    }

}


题目2

package SU;

/**

* @ClassName IsPrime

* @Description 判断101-200之间有多少个素数,并输出所有素数。

* @Author TumTam

* @Date 2019/8/8 14:58

* @Version 1.0

**/

public class IsPrime {

//修改为指定范围内的素数

    public  IsPrime(int min,int max){

int count =0;

        int n=min;

        int m=max;

        for(;n

Boolean b =true;

            for(int i=2;i

if(n%i==0){

b =false;

break;

                }

}

if(b==true){

count++;

                System.out.print(n+" ");

            }

}

System.out.println();

        System.out.println("在"+min+"和"+max+"范围内共有"+count+"个素数。");

    }

}


package SU;

import java.util.Scanner;

public class IsPrimeTest {

public static void main(String[] args) {

System.out.println("输入最大值、最小值确认范围来输出其中的素数:");

        Scanner num =new Scanner(System.in);

        int a=num.nextInt();

        int b=num.nextInt();

        num.close();

        int max;

        int min;

        if(a>b){

max = a;

          min = b;

        }else{

max = b;

            min = a;

        }

IsPrime ip =new IsPrime(min,max);

    }

}


题目3

package SU;

/**

* @ClassName NarcissisticNumber

* @Description 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。

* @Author TumTam

* @Date 2019/8/8 15:32

* @Version 1.0

**/

public class NarcissisticNumber {

//修改为指定范围的水仙花数

    public NarcissisticNumber(int min,int max){

int n = min;

        int m = max;

        int count =0;

        for(;n

int hundred = n /100;

            int decade = n /10%10;

            int unit = n %10;

            int total = hundred * hundred * hundred + decade * decade * decade + unit * unit * unit;

            if(n==total){

count++;

                System.out.print(n+" ");

            }

}

System.out.println("在"+min+"和"+max+"范围内共有"+count+"个水仙花数");

    }

}


package SU;

import java.util.Scanner;

public class NarcissisticNumberTest {

public static void main(String[] args) {

System.out.println("输入最大值、最小值确认范围来输出其中的水仙花数(三位数):");

        Scanner num =new Scanner(System.in);

        int a=num.nextInt();

        int b=num.nextInt();

        num.close();

        int max;

        int min;

        if(a>b){

max = a;

            min = b;

        }else{

max = b;

            min = a;

        }

NarcissisticNumber nn =new NarcissisticNumber(min,max);

    }

}


题目4

package SU;

/**

* @ClassName Factorization

* @Description 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5

* @Author TumTam

* @Date 2019/8/8 15:55

* @Version 1.0

**/

public class Factorization {

public Factorization(int num){

System.out.print(num+"=");

        int current = num;

        for(int i=2;i

if(current%i==0){

System.out.print(i+"*");

                current=current/i;

                i=1;

            }

}

System.out.print(current);

    }

}


package SU;

import java.util.Scanner;

public class FactorizationTest {

public static void main(String[] args) {

System.out.println("你想因式分解哪个因数?");

        Scanner num =new Scanner(System.in);

        int n = num.nextInt();

        num.close();

        Factorization f =new Factorization(n);

    }

}


题目5

package SU;

/**

* @ClassName ConditionalOperator

* @Description 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

* @Author TumTam

* @Date 2019/8/8 16:12

* @Version 1.0

**/

public class ConditionalOperator {

public ConditionalOperator(int score){

String Grade = (score>=90) ?"A" :((score>=60)?"B" :"C");

        System.out.println("当前分数所属等级为:"+Grade);

    }

}


package SU;

import java.util.Scanner;

public class ConditionalOperatorTest {

public static void main(String[] args) {

System.out.println("输入你的分数:");

        Scanner score =new Scanner(System.in);

        int s = score.nextInt();

        score.close();

        ConditionalOperator co =new ConditionalOperator(s);

    }

}


题目6

package SU;

/**

* @ClassName GreatestCommonDivisorAndLeastCommonMultiple

* @Description 输入两个正整数m和n,求其最大公约数和最小公倍数。

* @Author TumTam

* @Date 2019/8/8 16:24

* @Version 1.0

**/

public class GreatestCommonDivisorAndLeastCommonMultiple {

public GreatestCommonDivisorAndLeastCommonMultiple(int a,int b){

int GreatestCommonDivisor=1;

        int LeastCommonMultiple;

        int max = Max(a,b);

         for(int i=1;i

if(a%i==0 && b%i==0){

GreatestCommonDivisor=Max(GreatestCommonDivisor,i);

            }

}

LeastCommonMultiple = (a*b)/GreatestCommonDivisor;

        System.out.println(a+"和"+b+"的最大公约数为:"+GreatestCommonDivisor);

        System.out.println(a+"和"+b+"的最小公倍数为:"+LeastCommonMultiple);

    }

public int Max(int x,int y){

if(x>y){

return x;

        }else{

return y;

        }

}

}


package SU;

import java.util.Scanner;

public class GreatestCommonDivisorAndLeastCommonMultipleTest {

public static void main(String[] args) {

System.out.println("输入两个数求他们的最大公因数和最小公倍数。");

        Scanner Num =new Scanner(System.in);

        int m = Num.nextInt();

        int n = Num.nextInt();

        Num.close();

        GreatestCommonDivisorAndLeastCommonMultiple gcdANDlcm =new GreatestCommonDivisorAndLeastCommonMultiple(m,n);

    }

}


题目7

package SU;

/**

* @ClassName Statistics

* @Description 输入一行字符,分别统计出其英文字母、空格、数字和其它字符的个数。

* @Author TumTam

* @Date 2019/8/9 8:25

* @Version 1.0

**/

public class Statistics {

int chara =0,blank=0,number=0,others=0;

    public Statistics(String s){

for(int i=0;i

char c = s.charAt(i);

            if(c>='a'&&c<='z'){

chara++;

            }else if(c>'A'&&c<'Z'){

chara++;

            }else if(c==' '){

blank++;

            }else if(c>='0'&&c<='9'){

number++;

            }else{

others++;

            }

}

System.out.println("当前字符串里共有:");

        System.out.println("英文字母有"+chara+"个。");

        System.out.println("空格有"+blank+"个。");

        System.out.println("数字有"+number+"个。");

        System.out.println("其他字符有"+others+"个。");

    }

}


package SU;

import java.util.Scanner;

public class StatisticsTest {

public static void main(String[] args) {

System.out.println("请输入一串字符:");

        Scanner sentence =new Scanner(System.in);

        String s = sentence.nextLine();

        sentence.close();

        Statistics st =new Statistics(s);

    }

}


题目8

package SU;

/**

* @ClassName Sum

* @Description 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246;

* @Author TumTam

* @Date 2019/8/9 10:36

* @Version 1.0

**/

public class Sum {

//更改为输入的数字可以是任意数,可以输入重复次数,例如:

//请输入要使用的一个数:

//12

//请输入要使用的次数:

//3

//12+1212+121212=122436

    public Sum(int num,int times){

int sum=0;

        StringBuffer sb =new StringBuffer();

        for(int i=0;i

sb.append(num);

            String s = sb.toString();

            int a=Integer.parseInt(s);

            sum+=a;

            System.out.print(a);

            if(i<(times-1)){

System.out.print("+");

            }else{

System.out.print("=");

                System.out.print(sum);

            }

}

}

}


package SU;

import java.util.Scanner;

public class SumTest {

public static void main(String[] args) {

Scanner ctr =new Scanner(System.in);

        System.out.println("请输入要使用的一个数:");

        int num = ctr.nextInt();

        System.out.println("请输入要使用的次数:");

        int times = ctr.nextInt();

        ctr.close();

        Sum sum =new Sum(num,times);

    }

}


题目9

package SU;

/**

* @ClassName PerfectNumber

* @Description 一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。

* @Author TumTam

* @Date 2019/8/9 11:10

* @Version 1.0

**/

public class PerfectNumber {

public PerfectNumber(int min, int max){

int n=min;

        int m=max;

      for(;n

int current = n;

            int sum=1;

            int i=2;

            for(;i<=current;i++){

if(current%i==0){

sum+=i;

                    current=current/i;

                    i=1;

                }

}

if(n==sum){

System.out.print(n+" ");

            }

}

}

}


package SU;

import java.util.Scanner;

public class PerfectNumberTest {

public static void main(String[] args) {

System.out.println("输入最大值、最小值确认范围来输出其中的完数:");

        Scanner num =new Scanner(System.in);

        int a=num.nextInt();

        int b=num.nextInt();

        num.close();

        int max;

        int min;

        if(a>b){

max = a;

            min = b;

        }else{

max = b;

            min = a;

        }

PerfectNumber pn =new PerfectNumber(min,max);

    }

}


题目10


package SU;

/**

* @ClassName BallWay

* @Description 一球从h米高度自由落下,每次落地后反跳回原高度的一半;

* 再落下,求它在 第n次落地时,共经过多少米?第n次反弹多高?

* @Author TumTam

* @Date 2019/8/9 12:51

* @Version 1.0

**/

public class BallWay {

double height=0;

    double way=0;

    public  double Height(double h,int n) {

if (n ==1) {

return h/2;

        }else if(n==0){

return 0;

        }else{

height=h/2;

            h=height;

            n--;

            return Height(h,n);

        }

}

public double Way(double h,int n){

int num = n-1;

        way=way+h;

        //i从1开始,去除第一次落地的情况,第一次落地不用乘2,放在了上行。

      for(int i=1;i

way+=Height(h,num)*2;

          num--;

      }

return way;

    }

}


package SU;

import java.util.Scanner;

public class BallWayTest {

public static void main(String[] args) {

Scanner base =new Scanner(System.in);

        System.out.println("请输入初始高度:");

        double h = base.nextDouble();

        System.out.println("请输入次数:");

        int n = base.nextInt();

        base.close();

        BallWay bw =new BallWay();

        double height = bw.Height(h,n);

        double way = bw.Way(h,n);

        System.out.println("第"+n+"次弹起的高度为"+height);

        System.out.println("第"+n+"次落地经过的路程为"+way);

    }

}


题目11

package SU;

/**

* @ClassName FormFigure

* @Description 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

* @Author TumTam

* @Date 2019/8/9 14:50

* @Version 1.0

**/

public class FormFigure {

public FormFigure(){

int sum =0;

        for(int i=1;i<=4;i++){

for(int j=1;j<=4;j++){

for(int k=1;k<=4;k++){

if(i!=j && j!=k && k!=i){

int a=i*100+j*10+k;

                        System.out.print(a+" ");

                        sum++;

                        if(sum%6==0){

System.out.println();

                        }

}

}

}

}

System.out.println("共有"+sum+"个符合条件的三位数。" );

    }

}


package SU;

public class FormFigureTest {

public static void main(String[] args) {

FormFigure ff =new FormFigure();

    }

}


题目12

package SU;

import java.math.BigDecimal;

/**

* @ClassName Bonus

* @Description 企业发放的奖金根据利润提成。

* 利润(I)低于或等于10万元时,奖金可提10%;

* 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;

* 20万到40万之间时,高于20万元的部分,可提成5%;

* 40万到60万之间时高于40万元的部分,可提成3%;

* 60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,

* 从键盘输入当月利润I,求应发放奖金总数?

* @Author TumTam

* @Date 2019/8/9 15:08

* @Version 1.0

**/

public class Bonus {

int key1 =1000000;

    int key2 =600000;

    int key3 =400000;

    int key4 =200000;

    int key5 =100000;

    BigDecimalpercent =new BigDecimal(Integer.toString(0));

    BigDecimaldeal =new BigDecimal(Integer.toString(0));

    BigDecimaltemp =new BigDecimal(Integer.toString(0));

    BigDecimalbonus =new BigDecimal(Integer.toString(0));

    public BigDecimalBonus(double I){

int d =(int) I/key5;

        switch (d){

default:

case 10:

{

percent =new BigDecimal(Double.toString(0.01));

                deal =new BigDecimal(Double.toString(I -key1));

                temp =percent.multiply(deal);

                bonus =bonus.add(temp);

            }

case 9:

case 8:

case 7:

case 6:

{

percent =new BigDecimal(Double.toString(0.015));

                if (I >key1) {

deal =new BigDecimal(Integer.toString(key1 -key2));

                }else {

deal =new BigDecimal(Double.toString(I -key2));

                }

temp =percent.multiply(deal);

                bonus =bonus.add(temp);

            }

case 5:

case 4:

{

percent =new BigDecimal(Double.toString(0.03));

                if (I >key2) {

deal =new BigDecimal(Integer.toString(key2 -key3));

                }else {

deal =new BigDecimal(Double.toString(I -key3));

                }

temp =percent.multiply(deal);

                bonus =bonus.add(temp);

            }

case 3:

case 2:

{

percent =new BigDecimal(Double.toString(0.05));

                if (I >key3) {

deal =new BigDecimal(Integer.toString(key3 -key4));

                }else {

deal =new BigDecimal(Double.toString(I -key4));

                }

temp =percent.multiply(deal);

                bonus =bonus.add(temp);

            }

case 1:

{

percent =new BigDecimal(Double.toString(0.075));

                if (I >key4) {

deal =new BigDecimal(Integer.toString(key4 -key5));

                }else {

deal =new BigDecimal(Double.toString(I -key5));

                }

temp =percent.multiply(deal);

                bonus =bonus.add(temp);

            }

case 0:

{

percent =new BigDecimal(Double.toString(0.1));

                if (I >key5) {

deal =new BigDecimal(Integer.toString(key5));

                }else {

deal =new BigDecimal(Double.toString(I));

                }

temp =percent.multiply(deal);

                bonus =bonus.add(temp);

break;

            }

}

return bonus;

    }

}


package SU;

import java.math.BigDecimal;

import java.util.Scanner;

public class BonusTest {

public static void main(String[] args) {

Scanner ints =new Scanner(System.in);

        System.out.println("请输入总利润来计算奖金;");

        double I = ints.nextDouble();

        ints.close();

        Bonus b =new Bonus();

        BigDecimal bd = b.Bonus(I);

        System.out.println(I+"元共计可获得奖金"+bd+"元。");

    }

}


题目13

package SU;

/**

* @ClassName Judge

* @Description 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

* @Author TumTam

* @Date 2019/8/9 16:40

* @Version 1.0

**/

public class Judge {

int count =0;

    public Judge(int num){

int n = num+100;

        int m = n+168;

        boolean x = PerfectSquareNumber(n);

        boolean y = PerfectSquareNumber(m);

        if(x==true && y==true){

System.out.print(num+" ");

            count ++;

            if(count%10==0){

System.out.println();

            }

}

}

public boolean PerfectSquareNumber(int num){

boolean b =false;

        for(int i=0; i<=(num/2);i++){

int temp = i*i;

            if(num==temp){

b=true;

break;

            }

}

return b;

    }

}


package SU;

import java.util.Scanner;

public class JudgeTest {

public static void main(String[] args) {

System.out.println("请输入想找出多少以内符合条件的数:");

        Scanner max =new Scanner(System.in);

        int m = max.nextInt();

        max.close();

        System.out.println("符合条件的数有;");

        for(int i=0;i<=m;i++){

Judge j =new Judge(i);

        }

}

}


题目14

package SU;

/**

* @ClassName YearJudgment

* @Description 输入某年某月某日,判断这一天是这一年的第几天?

* @Author TumTam

* @Date 2019/8/12 8:28

* @Version 1.0

**/

public class YearJudgment {

public YearJudgment(){}

public YearJudgment(int year,int month,int day){

int J= Judgment(year,month,day);

        int total = day;

        for(int i=0;i<(month-1);i++){

total+= Judgment(year,1,day);

        }

System.out.println("这一天是这一年的第"+total+"天");

    }

public int Judgment(int year,int month,int day){

int []array = {31,28,31,30,31,30,31,31,30,31,30,31};

        if(month<=0|month>12){

return 0;

        }else{

if(year%4==0 | year % (-4) ==0){

array[1]=29;

                return array[month-1];

            }else{

array[1]=28;

                return array[month-1];

            }

}

}

}


package SU;

import java.util.Scanner;

public class YearJudgmentTest {

public static void main(String[] args) {

Scanner YMD =new Scanner(System.in);

        System.out.println("请输入年份:");

        int year = YMD.nextInt();

        System.out.println("请输入月份:");

        int month = YMD.nextInt();

        System.out.println("请输入具体日期:");

        int day = YMD.nextInt();

        YMD.close();

        YearJudgment yj =new YearJudgment();

        if(yj.Judgment(year,month,day) ==0 ){

System.out.println("你输入的月份有误!");

        }else if(day>yj.Judgment(year,month,day)|day<1){

System.out.println("你输入的日期有误!");

        }else{

YearJudgment YJ =new YearJudgment(year,month,day);

        }

}

}


题目15

package SU;

/**

* @ClassName Compare

* @Description 输入三个整数x,y,z,请把这三个数由小到大输出。

* @Author TumTam

* @Date 2019/8/12 14:37

* @Version 1.0

**/

public class Compare {

public Compare(double a,double b,double c){

if(a>b){

if(a>c) {

if(b>c){

System.out.println(a+">"+b+">"+c);

                }else{

System.out.println(a+">"+c+">"+b);

                }

}else{

System.out.println(c+">"+a+">"+b);

            }

}else{

if(a

                if(b

System.out.println(c+">"+b+">"+a);

                }else{

System.out.println(b+">"+c+">"+a);

                }

}else{

System.out.println(b+">"+a+">"+c);

            }

}

}

}


package SU;

import java.util.Scanner;

public class CompareTest {

public static void main(String[] args) {

Scanner number =new Scanner(System.in);

        double x=0;

        double y=0;

        double z=0;

        System.out.println("请输入三个数:");

        if(number.hasNextDouble()){

x = number.nextDouble();

            y = number.nextDouble();

            z = number.nextDouble();

        }else{

System.out.println("你输入的不是数字!");

        }

number.close();

        Compare c =new Compare(x,y,z);

    }

}


程序16

package SU;

/**

* @ClassName multiplication

* @Description 输出9*9口诀。

* @Author TumTam

* @Date 2019/8/12 15:06

* @Version 1.0

**/

public class Multiplication {

public Multiplication(){

for(int i=1;i<=9;i++){

for(int j=1;j<=i;j++){

System.out.print(j+"*"+i+"="+(j*i)+" ");

                if(i==j){

System.out.println();

                }

}

}

}

}


package SU;

public class multiplicationTest {

public static void main(String[] args) {

Multiplication Mp =new Multiplication();

    }

}


程序17

package SU;

/**

* @ClassName Monkey

* @Description 猴子吃桃问题:

* 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个

* 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。

* 以后每天早上都吃了前一天剩下的一半零一个。

* 到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

* @Author TumTam

* @Date 2019/8/12 15:24

* @Version 1.0

**/

public class Monkey {

int peach =0;

    public Monkey(){

for(int i=0;i<8;i++){

peach =(peach+1)*2;

        }

System.out.println("第一天共摘了"+peach+"个桃子");

    }

}


package SU;

public class MonkeyTest {

public static void main(String[] args) {

Monkey m =new Monkey();

    }

}


程序18

package SU;

import java.util.Arrays;

import java.util.List;

/**

* @ClassName Competition

* @Description 两个乒乓球队进行比赛,各出三人。

* 甲队为a,b,c三人,乙队为x,y,z三人。

* 已抽签决定比赛名单。有人向队员打听比赛的名单。

* a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

* @Author TumTam

* @Date 2019/8/12 15:36

* @Version 1.0

**/

public class Competition {

Stringa="a";

    Stringb="b";

    Stringc="c";

    StringBuffersb =new StringBuffer();

    StringBuffertemp =new StringBuffer();

    Listlist1 = Arrays.asList("a","b","c");

    Listlist2 = Arrays.asList("x","y","z");

    int count =0;

    public Competition(){

while(sb.indexOf(a)==-1 ||sb.indexOf(b)==-1 ||sb.indexOf(c) ==-1){

if(count>=9){

sb.setLength(0);

                count=0;

                int a = (int) (Math.random()*3);

                String T =list1.get(a);

                int b;

                if(a+1>2){

b = a+1-2;

                }else{

b=a+1;

                }

list1.set(a,list1.get(b));

                list1.set(b,T);

            }

for(String s:list1){

for(String k:list2){

if(sb.indexOf(s)==-1 &&sb.indexOf(k)==-1){

temp.setLength(0);

                        temp.append(s);

                        temp.append(k);

                        String t =temp.toString();

                        if(!"ax".equals(t) && !"cx".equals(t) && !"cz".equals(t)){

sb.append(t);

                            sb.append(" ");

                        }

}

count++;

                }

}

}

System.out.println(sb.toString());

    }

}


package SU;

public class CompetitionTest {

public static void main(String[] args) {

System.out.println("三队赛手的名单:");

        Competition c =new Competition();

    }

}


题目19

package SU;

/**

* @ClassName PrintS

* @Description 打印出如下图案(菱形)

*

*    *

*    ***

*  ******

* ********

*  ******

*  ***

*    *

* @Author TumTam

* @Date 2019/8/13 8:28

* @Version 1.0

**/

public class PrintS {

int star =1;

    public PrintS(){

for(int i=1;i<8;i++){

int a = Math.abs(i-4);

          for(int j=0;j

System.out.print(" ");

          }

for (int k=0;k

System.out.print("*");

          }

System.out.println();

          if(i<4){

star+=2;

          }else {

star-=2;

          }

}

}

}


package SU;

public class PrintSTest {

public static void main(String[] args) {

PrintS ps =new PrintS();

    }

}


题目20

package SU;

/**

* @ClassName SumFraction

* @Description 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13... 求出这个数列的前20项之和。

* @Author TumTam

* @Date 2019/8/13 8:46

* @Version 1.0

**/

public class SumFraction {

int sum =0 ;

    public SumFraction(int n){

int [] numerator =new int[n];

        int [] denominator =new int[n];

        numerator[0]=2;

        numerator[1]=3;

        denominator[0]=1;

        denominator[1]=2;

        for(int i=2;i

numerator[i] = numerator[i-1]+numerator[i-2];

            denominator[i] = denominator[i-1]+denominator[i-2];

        }

for(int j=0;j

sum+=(numerator[j]/denominator[j]);

        }

System.out.println(sum);

    }

}


package SU;

import java.util.Scanner;

public class SumFractionTest {

public static void main(String[] args) {

System.out.println("请输入想要求前几个数的和:");

        Scanner fra =new Scanner(System.in);

        int n = fra.nextInt();

        fra.close();

        SumFraction sf =new SumFraction(n);

    }

}


题目21

package SU;

/**

* @ClassName Factorial

* @Description 求1+2!+3!+...+20!的和

* @Author TumTam

* @Date 2019/8/16 14:56

* @Version 1.0

**/

public class Factorial {

public int sum(int a){

int sum =0;

        for(int i=1;i<=a;i++){

sum = sum +factorial(i);

        }

return sum;

    }

public int factorial(int a){

int result=1;

        for(int i=1;i<=a;i++){

result = result*i;

        }

return result;

    }

}


package SU;

import java.util.Scanner;

public class FactorialTest {

public static void main(String[] args) {

System.out.println("你想求前多少位的阶乘之和?");

        Scanner s =new Scanner(System.in);

        int q = s.nextInt();

        s.close();

        Factorial f =new Factorial();

        int sum = f.sum(q);

        System.out.println("前"+q+"位的阶乘之和为"+sum);

    }

}


题目22

package SU;

/**

* @ClassName RecursionFactorial

* @Description 利用递归方法求5!

* @Author TumTam

* @Date 2019/8/16 15:11

* @Version 1.0

**/

public class RecursionFactorial {

int c =1;

    public int count(int a){

if(a==1){

return c;

        }else {

return a*count(a-1);

        }

}

}


package SU;

import java.util.Scanner;

public class RecursionFactorialTest {

public static void main(String[] args) {

System.out.println("你想求谁的阶乘?");

        Scanner sr =new Scanner(System.in);

        int v = sr.nextInt();

        sr.close();

        RecursionFactorial rf =new RecursionFactorial();

        int s = rf.count(v);

        System.out.println(v+"的阶乘为"+s);

    }

}


题目23

package SU;

/**

* @ClassName RecursionProblem

* @Description 有5个人坐在一起,问第五个人多少岁?

* 他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。

* 问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。

* 最后问第一个人,他说是10岁。请问第五个人多大?

* @Author TumTam

* @Date 2019/8/16 15:31

* @Version 1.0

**/

public class RecursionProblem {

public int RecursionProblem(int a,int b){

if(a==1){

return 10;

        }else{

return RecursionProblem(a-1,b)+b;

        }

}

}


package SU;

public class RecursionProblemTest {

public static void main(String[] args) {

RecursionProblem rp =new RecursionProblem();

        int a = rp.RecursionProblem(5,2);

        System.out.println("第五个人"+a+"岁");

    }

}


题目24 

package SU;

import java.util.ArrayList;

import java.util.List;

/**

* @ClassName PositiveInteger

* @Description 给一个不多于5位的正整数,

* 要求:一、求它是几位数,二、逆序打印出各位数字。

* @Author TumTam

* @Date 2019/8/16 15:49

* @Version 1.0

**/

public class PositiveInteger {

public PositiveInteger(int a){

int gw = a%10;

        int sw = (a/10)%10;

        int bw = (a/100)%10;

        int qw = (a/1000)%10;

        int ww = (a/10000)%10;

        List list =new ArrayList<>(16);

        if(ww !=0){

list.add(gw);

        }

if(qw !=0){

list.add(sw);

        }

if(bw !=0){

list.add(bw);

        }

if(sw !=0){

list.add(qw);

        }

if(gw !=0){

list.add(ww);

        }

int length = list.size();

        System.out.println("该数字为"+length+"位数。");

        System.out.println("逆序输出结果为:");

        for(Integer l : list){

System.out.print(l);

        }

}

}


package SU;

import java.util.Scanner;

public class PositiveIntegerTest {

public static void main(String[] args) {

System.out.println("请输入一个5位以内的数字:");

        Scanner sc =new Scanner(System.in);

        int shu = sc.nextInt();

        sc.close();

        PositiveInteger pi =new PositiveInteger(shu);

    }

}


题目25

package SU;

import java.util.ArrayList;

import java.util.List;

/**

* @ClassName PoInteger

* @Description 一个5位数,判断它是不是回文数。

* 即12321是回文数,个位与万位相同,十位与千位相同。

* @Author TumTam

* @Date 2019/8/19 11:23

* @Version 1.0

**/

public class PoInteger {

public PoInteger(int a){

int gw = a%10;

        int sw = (a/10)%10;

        int bw = (a/100)%10;

        int qw = (a/1000)%10;

        int ww = (a/10000)%10;

        List list =new ArrayList<>(16);

        if(ww !=0){

list.add(gw);

        }

if(qw !=0){

list.add(sw);

        }

if(bw !=0){

list.add(bw);

        }

if(sw !=0){

list.add(qw);

        }

if(gw !=0){

list.add(ww);

        }

int x= list.size()/2+1;

        int count =0;

        for(int i =0; i

int j = list.size()-1-i;

            if(list.get(i).equals(list.get(j))){

count ++;

            }

}

if(count ==3){

System.out.println("这是一个回文数。");

        }else {

System.out.println("这不是一个回文数。");

        }

}

}


package SU;

import java.util.Scanner;

public class PoIntegerTest {

public static void main(String[] args) {

System.out.println("请输入一个5位数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        sc.close();

        PoInteger pi =new PoInteger(a);

    }

}


题目26

package SU;

/**

* @ClassName WeekJug

* @Description 请输入星期几的第一个字母来判断一下是星期几,

* 如果第一个字母一样,则继续判断第二个字母。

* @Author TumTam

* @Date 2019/8/19 12:57

* @Version 1.0

**/

public class WeekJug {

public StringFirWeekJug(String s){

StringD="";

        switch (s){

case "m":

D ="Monday";

break;

            case "w":

D ="Wednesday";

break;

            case "f":

D ="Friday";

break;

            case "t":

D ="1";

break;

            case "s":

D ="2";

break;

        }

return D;

    }

public StringSceWeekJug(String s){

StringD="";

        switch (s) {

case "u":

D ="Tuesday";

break;

            case "h":

D ="Thursday";

break;

        }

return D;

    }

public StringThrWeekJug(String s){

StringD="";

        switch (s) {

case "a":

D ="Saturday";

break;

            case "u":

D ="Sunday";

break;

        }

return D;

    }

}


package SU;

import java.util.Scanner;

public class WeekJugTest {

public static void main(String[] args) {

System.out.println("请输入第一个字母:");

        Scanner sr =new Scanner(System.in);

        String s = sr.next();

        WeekJug wj =new WeekJug();

        String sc = wj.FirWeekJug(s);

        if("1"==sc ||"2"==sc){

System.out.println("请输入第二个字母:");

            s = sr.next();

            sr.close();

            if("1"==sc){

sc = wj.SceWeekJug(s);

                System.out.println(sc);

            }else{

sc = wj.ThrWeekJug(s);

                System.out.println(sc);

            }

}else{

System.out.println(sc);

        }

sr.close();

    }

}


题目27

package SU;

/**

* @ClassName PrimeNumber

* @Description 求100之内的素数

* @Author TumTam

* @Date 2019/8/19 13:44

* @Version 1.0

**/

public class PrimeNumber {

public PrimeNumber(int a){

int count=0;

        for(int i=3;i<=a;i++){

boolean b =true;

            for(int j =2;j<=(i-1);j++){

if((i%j)==0){

b =false;

break;

                }

}

if(true == b){

System.out.print(i+" ");

                count++;

                if(count%20==0){

System.out.println();

                }

}

}

}

}


package SU;

import java.util.Scanner;

public class PrimeNumberTest {

public static void main(String[] args) {

System.out.println("你想求多少以内的素数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        System.out.println(a+"之内的素数有:");

        System.out.print("1"+" ");

        PrimeNumber pn =new PrimeNumber(a);

    }

}


题目28

package SU;


/**

 * @ClassName DeSort

 * @Description 对10个数进行排序

 * @Author TumTam

 * @Date 2019/8/19 14:23

 * @Version 1.0

 **/

public class DeSort {

    public DeSort(int []a){

          for(int w=0;w

        {

            for(int v=0;v<=a.length-w-2;v++)

            {

                int m=0;

                if(a[v]>a[v+1])

                {

                    m=a[v];

                    a[v]=a[v+1];

                    a[v+1]=m;

                }

            }

        }

        for(int f=0;f

        {

            System.out.print(a[f]+" ");

        }

    }

}


package SU;

import java.util.Scanner;

public class DeSortTest {

public static void main(String[] args) {

System.out.println("你想输入几个数?");

        Scanner t =new Scanner(System.in);

        int r=t.nextInt();

        int a[]=new int[r];

        System.out.println("请输入:");

        Scanner n =new Scanner(System.in);

      for(int f=0;f

{

a[f]=n.nextInt();

      }

DeSort ds =new DeSort(a);

    }

}


题目29

package SU;

/**

* @ClassName Matrix

* @Description 求一个3*3矩阵对角线元素之和

* @Author TumTam

* @Date 2019/8/19 14:33

* @Version 1.0

**/

public class Matrix {

public Matrix(int [][]a){

int s1 = a[0][0] + a[1][1] + a[2][2];

        int s2 = a[0][2] + a[1][1] + a[2][0];

        System.out.println("第一条对角线的值为:"+s1);

        System.out.println("第二条对角线的值为:"+s2);

    }

}


package SU;

import java.util.Scanner;

public class MatrixTest {

public static void main(String[] args) {

System.out.println("请输入9个数字:");

        Scanner sc =new Scanner(System.in);

        int [][]a =new int[3][3];

        for(int i=0;i<3;i++){

for(int j=0;j<3;j++){

a[i][j]=sc.nextInt();

            }

}

sc.close();

        Matrix m =new Matrix(a);

    }

}


题目30

package SU;

import org.apache.commons.lang3.ArrayUtils;

/**

* @ClassName InsertA

* @Description 有一个已经排好序的数组。

* 现输入一个数,要求按原来的规律将它插入数组中。

* @Author TumTam

* @Date 2019/8/19 15:00

* @Version 1.0

**/

public class InsertA {

public InsertA(int a){

int []y = {1,5,9,26,33,65,98,103,235,468,705,987,2065};

        System.out.println("插入数前的数组为:");

        for(int s:y){

System.out.print(s+" ");

        }

int []x =new int[0];

        for(int i=0;(i+1)

if(y[i]

x=ArrayUtils.add(x,y[i]);

                x=ArrayUtils.add(x,a);

            }else{

x=ArrayUtils.add(x,y[i]);

            }

}

x=ArrayUtils.add(x,y[y.length-1]);

        System.out.println();

        System.out.println("插入数字后的数组为:");

        for(int s:x){

System.out.print(s+" ");

        }

}

}


package SU;

import java.util.Scanner;

public class InsertATest {

public static void main(String[] args) {

System.out.println("请输入一个数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        InsertA ia =new InsertA(a);

    }

}


题目31

package SU;


/**

 * @ClassName ReverseOrder

 * @Description 将一个数组逆序输出。

 * @Author TumTam

 * @Date 2019/8/19 15:35

 * @Version 1.0

 **/

public class ReverseOrder {

    public ReverseOrder(int []a){

//        int []b = new int[a.length];

//        for(int i=0;i

//            b[i]=a[a.length-1-i];

//        }

//

//        for(int x:b){

//            System.out.print(x+" ");

//        }


        for(int i=0;i

            System.out.print(a[a.length-1-i]+" ");

        }

    }

}



package SU;

import java.util.Scanner;

public class ReverseOrderTest {

public static void main(String[] args) {

System.out.println("你想输入几个数?");

        Scanner t =new Scanner(System.in);

        int r=t.nextInt();

        int a[]=new int[r];

        System.out.println("请输入:");

        for(int f=0;f

{

a[f]=t.nextInt();

        }

t.close();

        System.out.println("倒序输出:");

        ReverseOrder ro =new ReverseOrder(a);

    }

}


题目32

package SU;

/**

* @ClassName GetValue

* @Description TODO

* @Author TumTam

* @Date 2019/8/21 8:34

* @Version 1.0

**/

public class GetValue {

public GetValue(String sum){

StringBuffer sb =new StringBuffer();

        String temp;

        int result;

        sb.append(sum);

        temp = sb.reverse().toString().substring(4,8);

        sb.setLength(0);

        sb.append(temp);

        temp =null;

        temp = sb.reverse().toString();

        result = Integer.parseInt(temp);

        System.out.println("你输入数字从右开始的4到7位为:"+result);

    }

}


package SU;

import java.util.Scanner;

public class GetValueTest {

public static void main(String[] args) {

System.out.println("请输入一个7位以上的数字:");

        Scanner sd =new Scanner(System.in);

        String num = sd.next();

        int length = num.length();

        sd.close();

        if(length<7){

System.out.println("你输入的数字少于7位!");

        }else{

GetValue gv =new GetValue(num);

        }

}

}


题目33

package SU;



/**

 * @ClassName PascalTriangle

 * @Description 打印出杨辉三角形(要求打印出10行如下图)

 * @Author TumTam

 * @Date 2019/8/23 8:44

 * @Version 1.0

 **/

public class PascalTriangle {

    public PascalTriangle(int a){

        int x = 2*a;

        int[][] array = new int [x][x];

        int middle = (x/2)+1;

        array[0][middle] = 1;

        for(int i=1;i

            for(int j=1;j

                array[i][j-1] = array[i-1][j-1] + array[i-1][j];

            }


        }


        for(int i=0;i

            for(int j=0;j

                if(array[i][j]==0){

                    System.out.print(" ");

                }else{

                    System.out.print(array[i][j]+" ");

                }

            }

            System.out.println();

        }

    }

}


package SU;

import java.util.Scanner;

public class PascalTriangleTest {

public static void main(String[] args) {

System.out.println("你想输入几行杨辉三角:");

        Scanner sc =new Scanner(System.in);

        int num = sc.nextInt();

        sc.close();

        PascalTriangle pt =new PascalTriangle(num);

    }

}


题目34

package SU;




/**

 * @ClassName OrderOutput

 * @Description 输入3个数a,b,c,按大小顺序输出。

 * @Author TumTam

 * @Date 2019/8/23 11:30

 * @Version 1.0

 **/

public class OrderOutput {

    public OrderOutput(int a,int b,int c) {

        int temp;

            if(b

                temp = b;

                b = c;

                c = temp;

            }

            if(a

                temp = a;

                a = c;

                c = temp;

            }

            if(a

                temp = a;

                a = b;

                b = temp;


            }

        System.out.println("从大到小输出:");

        System.out.println(a+">"+b+">"+c);


    }


    }



package SU;

import java.util.Scanner;

public class OrderOutputTest {

public static void main(String[] args) {

System.out.println("请输入三个数:");

        Scanner sss =new Scanner(System.in);

        int a = sss.nextInt();

        int b = sss.nextInt();

        int c = sss.nextInt();

        sss.close();

        OrderOutput oo =new OrderOutput(a,b,c);

    }

}


题目35

package SU;


import static java.lang.Integer.MAX_VALUE;

import static java.lang.Integer.MIN_VALUE;


/**

 * @ClassName ArrayChange

 * @Description 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

 * @Author TumTam

 * @Date 2019/8/26 12:44

 * @Version 1.0

 **/

public class ArrayChange {

    public ArrayChange(int[] array){

        int max = MIN_VALUE;

        int maxid = 0;

        int min = MAX_VALUE;

        int minid = 0;

        int temp;

        for(int i=0;i

            if(array[i]>max){

                max = array[i];

                maxid = i;

            }

            if(array[i]

                min = array[i];

                minid = i;

            }

        }

        temp = array[0];

        array[0] = array[maxid];

        array[maxid] = temp;


        temp = array[array.length-1];

        array[array.length-1] = array[minid];

        array[minid] = temp;


        for(int a:array){

            System.out.print(a+" ");

        }


    }

}



package SU;



import java.util.Scanner;


public class ArrayChangeTest {

    public static void main(String[] args) {

        System.out.println("你想输入几个值:");

        Scanner dc = new Scanner(System.in);

        int a = dc.nextInt();

        int[] array = new int[a];

        System.out.println("请输入:");

        for(int i=0;i

            array[i] = dc.nextInt();

        }

        dc.close();

        ArrayChange ac = new ArrayChange(array);

    }

}

题目36


package SU;


/**

 * @ClassName ArrayMove

 * @Description 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

 * @Author TumTam

 * @Date 2019/8/26 13:09

 * @Version 1.0

 **/

public class ArrayMove {

    public ArrayMove(int[] array,int m){

        int[] arraylist = new int[array.length];

        for(int i=0;i

            arraylist[i] = array[array.length-m+i];

        }

        for(int i=m;i

            arraylist[i] = array[i-m];

        }


        System.out.println("变更后的结果为:");

        for(int a:arraylist){

            System.out.println(a+" ");

        }

    }

}



package SU;



import java.util.Scanner;


public class ArrayMoveTest {

    public static void main(String[] args) {

        System.out.println("你要输入几个数:");

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();

        int[] array = new int[a];

        System.out.println("请输入:");

        for(int i=0;i

            array[i] = sc.nextInt();

        }

        System.out.println("请问想要将前几位数后移:");

        int m = sc.nextInt();

        sc.close();

        if(m>array.length){

            m = m - array.length;

        }

        ArrayMove am = new ArrayMove(array,m);

    }

}

题目37

package SU;


import java.util.HashMap;

import java.util.Map;


/**

 * @ClassName NumberOff

 * @Description 有n个人围成一圈,顺序排号。

 * 从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,

 * 问最后留下的是原来第几号的那位。

 * @Author TumTam

 * @Date 2019/8/26 13:44

 * @Version 1.0

 **/

public class NumberOff {

    public NumberOff(int num){

        Map map = new HashMap<>(num);

        for(int i=0;i

            map.put((i+1),true);

        }

        int b = num;

        int count=1;

        int cun = 0;

        while(b>1){

            for(int i=0;i

                int x=i+1;

                if(map.get(x)){

                    if(count==3){

                        map.put(x,false);

                        b--;

                        count = 1;

                    }else{

                        count++;

                    }

                }

            }

        }

        for(int i:map.keySet()){

            if(map.get(i)){

                System.out.println("最后一人为原先的"+i+"号");

            }

        }

    }

}



package SU;

import java.util.Scanner;

public class NumberOffTest {

public static void main(String[] args) {

System.out.println("请输入人数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        NumberOff no =new NumberOff(a);

    }

}


题目38

package SU;

/**

* @ClassName LengthCount

* @Description 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。

* @Author TumTam

* @Date 2019/8/26 14:39

* @Version 1.0

**/

public class LengthCount {

public LengthCount(StringBuffer sb){

int num = sb.length();

        System.out.println("字符串的长度为:"+num);

    }

}


package SU;

import java.util.Scanner;

public class LengthCountTest {

public static void main(String[] args) {

System.out.println("请输入字符串:");

        Scanner sc =new Scanner(System.in);

        String s = sc.nextLine();

        sc.close();

        StringBuffer sb =new StringBuffer(s);

        LengthCount lc =new LengthCount(sb);

    }

}


题目39

package SU;


import org.apache.commons.lang3.ArrayUtils;


/**

 * @ClassName OddNumberOrNot

 * @Description 编写一个函数,输入n为偶数时。

 * 调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)

 * @Author TumTam

 * @Date 2019/8/26 15:04

 * @Version 1.0

 **/

public class OddNumberOrNot {

    public OddNumberOrNot(int num){

        int b = 2;

        int[] array = new int[0];

        double sum = 0;

        if(num%2==0){

            for(int i =b;i<=num;i+=b){

                array = ArrayUtils.add(array,i);

            }

        }else{

            for(int i =1;i<=num;i+=b){

                array = ArrayUtils.add(array,i);

            }

        }


        for(int i=0;i

            int x = array[0];

            double t =(double) 1/x;

            sum=sum + t;

            System.out.print(("1/"+x)+"+");

        }

        int  y = array[array.length-1];

        double te = (double) 1/y;

        System.out.print("1/"+y+"="+(sum+te));

    }

}



package SU;

import java.util.Scanner;

public class OddNumberOrNotTest {

public static void main(String[] args) {

System.out.println("请输入一个数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        sc.close();

        OddNumberOrNot onon =new OddNumberOrNot(a);

    }

}


题目40

package SU;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

/**

* @ClassName StringSort

* @Description 字符串排序。

* @Author TumTam

* @Date 2019/8/26 16:23

* @Version 1.0

**/

public class StringSort {

public StringSort(List list){

Collections.sort(list, new Comparator() {

@Override

            public int compare(String o1, String o2) {

return o1.compareTo(o2);

            }

});

        for(String key : list){

System.out.print(key+" ");

        }

}

}


package SU;




import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class StringSortTest {

    public static void main(String[] args) {

        List list = new ArrayList<>(16);

        System.out.println("你想输入几个字符串:");

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();

        System.out.println("请输入:");

        for(int i=0;i

            list.add(sc.next());

        }

       StringSort ss = new StringSort(list);

    }


}


题目41

package SU;

/**

* @ClassName MonkeyAndPeach

* @Description 海滩上有一堆桃子,五只猴子来分。

* 第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。

* 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,

* 第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

* @Author TumTam

* @Date 2019/9/6 8:59

* @Version 1.0

**/

public class MonkeyAndPeach {

int sum =0;

    public int countPeach(int n){

if(n==1){

return 1;

        }else{

n--;

            sum+=(countPeach(n)*5+1);

            return sum;

        }

}

}


package SU;

public class MonkeyAndPeachTest {

public static void main(String[] args) {

MonkeyAndPeach map =new MonkeyAndPeach();

        int a =map.countPeach(5);

        System.out.println("原本最少有"+a+"个桃子。");

    }

}


题目42

package SU;

/**

* @ClassName ForAnswer

* @Description 809*??=800*??+9*??+1

* 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。

* 求??代表的两位数,及809*??后的结果。

* @Author TumTam

* @Date 2019/9/6 10:48

* @Version 1.0

**/

public class ForAnswer {

public void askQuestion(){

System.out.println("符合要求的数有:");

        for(int i=10;i<100;i++){

int left =809*i;

            int right =800*i+9*i+1;

            int r1 =8*i;

            int r2 =9*i;

            boolean a = (left == right);

            boolean b = (Judget(r1)==2);

            boolean c = (Judget(r2)==3);

            if(a && b && c){

System.out.print(i+" ");

            }else{

System.out.println("没有符合要求的数。");

            }

}

}

public int Judget(int a){

StringBuffer sb =new StringBuffer();

        sb.append(a);

        int len = sb.length();

        return len;

    }

}


package SU;

public class ForAnswerTest {

public static void main(String[] args) {

ForAnswer fa =new ForAnswer();

        fa.askQuestion();

    }

}


题目43

package SU;

/**

* @ClassName DiyOdd

* @Description 求0—7所能组成的奇数个数。

* @Author TumTam

* @Date 2019/9/6 11:12

* @Version 1.0

**/

public class DiyOdd {

public int sumOdd(){

int sum =0;

        for(int i=1;i<=8;i++){

if(i==1){

sum+=4;

            }else if(i==2){

sum+=(4*7);

            }else{

int temp = ressult(i-2);

                sum+=(4*7*temp);

            }

}

return sum;

    }

public int ressult(int n){

int r =1;

        for(int i=0;i

r = r*8;

        }

return r;

    }

}


package SU;

public class DiyOddTest {

public static void main(String[] args) {

DiyOdd dod =new DiyOdd();

        int all = dod.sumOdd();

        System.out.println("一共可以组成"+all+"个奇数。");

    }

}


题目44


package SU;



/**

 * @ClassName EvenResolve

 * @Description 一个偶数总能表示为两个素数之和。

 * @Author TumTam

 * @Date 2019/9/6 12:42

 * @Version 1.0

 **/

public class EvenResolve {

    public boolean dept(int a){

        boolean bo;

        if(a%2 == 0){

            bo = true;

        }else{

            bo = false;

        }

        return bo;

    }


    public boolean prim(int b){

        boolean bo = true;

        for(int i=2;i

            if(b%i==0){

                bo = false;

                break;

            }

        }

        return bo;

    }


    public void resolve(int c){

        boolean a = dept(c);

        int count = 0;


        if(a){

            for(int i=1;i<(c/2);i++){

                boolean b = prim(i);

                if(b){

                    int bb = c-i;

                    boolean p = prim(bb);

                    if(p){

                        System.out.println(c+"="+i+"+"+bb);

                        count++;

                    }

                }

            }

            if(count==0){

                System.out.println("此偶数无法分解为素数加法。");

            }

        }else{

            System.out.println("这不是一个偶数。");

        }

    }


}



package SU;

import java.util.Scanner;

public class EvenResolveTest {

public static void main(String[] args) {

System.out.println("请输入一个偶数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        sc.close();

        EvenResolve er =new EvenResolve();

        er.resolve(a);

    }

}


题目45

package SU;


/**

 * @ClassName DivideExactly

 * @Description 判断一个素数能被几个9整除

 * 首先,这个题目我就看不懂,素数怎么被9整除?

 * 所以就忽略整除这个点了,不然真不知道怎么做。

 * @Author TumTam

 * @Date 2019/9/6 13:20

 * @Version 1.0

 **/

public class DivideExactly {


    public boolean prim(int b){

        boolean bo = true;

        for(int i=2;i

            if(b%i==0){

                bo = false;

                break;

            }

        }

        return bo;

    }


    public void sout(int a){

        boolean b = prim(a);

        int countt = 0;

        if(b){

            while(a>=9){

                a = a/9;

                countt++;

            }

        }else{

            System.out.println("你输入的不是素数。");

        }

        System.out.println("该素数比9的"+countt+"次方大");

    }

}



package SU;

import java.util.Scanner;

public class DivideExactlyTest {

public static void main(String[] args) {

System.out.println("请输入一个素数:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        sc.close();

        DivideExactly de =new DivideExactly();

        de.sout(a);

    }

}


题目46

package SU;


/**

 * @ClassName StringLink

 * @Description 两个字符串连接程序

 * @Author TumTam

 * @Date 2019/9/6 13:47

 * @Version 1.0

 **/

public class StringLink {

    public String stringResult(String s1,String s2){

        String str = s1 + s2;

        return str;

    }

}


package SU;



import java.util.Scanner;


public class StringLinkTest {


    public static void main(String[] args) {

        System.out.println("请输入两个字符串:");

        Scanner sc = new Scanner(System.in);

        String a = sc.next();

        String b = sc.next();

        sc.close();

        StringLink sl = new StringLink();

        String s = sl.stringResult(a,b);

        System.out.println(s);

    }

}


题目47

package SU;


/**

 * @ClassName PrintStars

 * @Description 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

 * @Author TumTam

 * @Date 2019/9/6 13:57

 * @Version 1.0

 **/

public class PrintStars {

    public void printStar(int a){

        for(int i=0;i

            System.out.print("*");

        }

        System.out.println();

    }

}



package SU;



import java.util.Scanner;


public class PrintStarsTest {


    public static void main(String[] args) {

        System.out.println("请输入一到五十以内的7个整数:");

        Scanner sc = new Scanner(System.in);

        PrintStars ps = new PrintStars();

        for(int i=0;i<7;i++){

            ps.printStar(sc.nextInt());

        }

            sc.close();

    }

}


题目48

package SU;


/**

 * @ClassName encryption

 * @Description 某个公司采用公用电话传递数据,数据是四位的整数,

 * 在传递过程中是加密的,加密规则如下:

 * 每位数字都加上5,然后用和除以10的余数代替该数字,

 * 再将第一位和第四位交换,第二位和第三位交换。

 * @Author TumTam

 * @Date 2019/9/6 14:57

 * @Version 1.0

 **/

public class encryption {

    public String encry(int a) {

        StringBuffer sb = new StringBuffer();

        sb.append(a);

        for (int i = 0; i < 4; i++) {

            char b = sb.charAt(i);

            int cc = b + 5 -'0';

            int ccc = cc%10;

            char c = String.valueOf(ccc).charAt(0);

            sb.setCharAt(i, c);

        }

        char c = sb.charAt(0);

        sb.setCharAt(0, sb.charAt(3));

        sb.setCharAt(3, c);

        char b = sb.charAt(1);

        sb.setCharAt(1, sb.charAt(2));

        sb.setCharAt(2, b);

        String str = sb.toString();

        return str;

    }

}


package SU;



import java.util.Scanner;


public class encryptionTest {


    public static void main(String[] args) {

        System.out.println("请输入一个四位数:");

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();

        sc.close();

        encryption e = new encryption();

        String s = e.encry(a);

        System.out.println(s);

    }

}


题目49

package SU;


/**

 * @ClassName CharCount

 * @Description 计算字符串中子串出现的次数

 * @Author TumTam

 * @Date 2019/9/9 8:31

 * @Version 1.0

 **/

public class CharCount {

    public int charC(String c){

        int num =0;

        String[] s = c.split(" ");

        num = s.length;

        return num;

    }

}


package SU;



import java.util.Scanner;


public class CharCountTest {


    public static void main(String[] args) {

        System.out.println("请输入多条字符串,以空格分隔:");

        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();

        sc.close();

        CharCount cc = new CharCount();

        int num = cc.charC(s);

        System.out.println("该字符串中有"+num+"个子字符串。");

    }

}



题目50

package SU;


import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.Iterator;

import java.util.LinkedHashMap;

import java.util.List;



/**

 * @ClassName IO

 * @Description 有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"stud"中。

 * @Author TumTam

 * @Date 2019/9/9 8:44

 * @Version 1.0

 **/

public class IO {

    public void doIO(List> list) throws IOException {

        File file = new File("D:\\stud.txt");

        FileOutputStream out = new FileOutputStream(file,true);

        OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");

        for(LinkedHashMap map :list){

            Iterator it = map.keySet().iterator();

            while(it.hasNext()){

                String key = it.next();

                String str = key+":"+map.get(key);

                osw.write(str+"\r\n");

            }

            osw.write("\r\n");

        }

        osw.close();

    }

}



package SU;

import java.io.IOException;

import java.util.*;

public class IOTest {

public static void main(String[] args)throws IOException {

System.out.println("请输入你想录入多少学生的成绩:");

        Scanner sc =new Scanner(System.in);

        int a = sc.nextInt();

        List> list =new ArrayList<>();

        for(int i=0;i

LinkedHashMap map =new LinkedHashMap<>(16);

            System.out.println("输入第"+(i+1)+"个学生的信息:");

            System.out.println("学号:");

            map.put("学号",sc.next());

            System.out.println("姓名:");

            map.put("姓名",sc.next());

            System.out.println("语文成绩:");

            String f = sc.next();

            map.put("语文成绩",f);

            System.out.println("数学成绩:");

            String s = sc.next();

            map.put("数学成绩",s);

            System.out.println("英语成绩:");

            String t = sc.next();

            map.put("英语成绩",t);

            int avg = (Integer.parseInt(f)+Integer.parseInt(s)+Integer.parseInt(t))/3;

            String avge = avg +"";

            map.put("平均成绩",avge);

            list.add(i,map);

        }

IO io =new IO();

        io.doIO(list);

    }

}


终于写完了,如果哪里有错有缺失,欢迎指正~

你可能感兴趣的:(Java编程练习题)