Java经典编程题50道之二十六】的更多相关文章

请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母. public class Example26 {    public static void main(String[] args) {        f();    } public static void f() {        System.out.println("请输入星期的第一个大写字母:");        char ch = getChar();        switch (ch…
一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. public class Example25 {    public static void main(String[] args) {        f2(123454321);    }//方法一    public static void f1(int n) {        if (n >= 10000 && n < 100000) {            String s = S…
有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. public class Example20 {    public static void main(String[] args) {        sum(20);    } public static void sum(int n) {        double x = 2.0;        double y = 1.0;        double t;        double…
求一个3*3矩阵对角线元素之和. public class Example29 {    public static void main(String[] args) {        int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };        sum(a);    } public static void sum(int[][] a) {        System.out.println("您输入的3*3矩阵是:");…
对10个数进行排序. public class Example28 {    public static void main(String[] args) {        int[] s = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };        BubbleSort(s);    } public static void BubbleSort(int[] a) {        System.out.print("原数组为:");        for (int…
有5个人坐在一起,问第5个人多少岁,他说比第4个人大2岁.问第4个人岁数,他说比第3个人大2岁. 问第三个人,他说比第2人大两岁.问第2个人, 说比第一个人大两岁.最后问第一个人,他说是10岁. 请问第五个人多大? public class Example24{    public static void main(String[] args) {        age();    } public static void age() {        int age = 10;        …
利用递归方法求5!. public class Example22 {    public static void main(String[] args) {        int n = 5;        long s = sum(n);        System.out.println(n + "!= " + s);    } public static long sum(int n) {        long s = 1;        if (n == 1||n==0)…
求100之内的素数. public class Example27 {    public static void main(String[] args) {        prime();    } public static void prime() {        System.out.print(2 + "\t");        System.out.print(3 + "\t");        int count = 2;        boolea…
给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. public class Example23 {    public static void main(String[] args) {        f(123789);    } public static void f(long l) {        String s = Long.toString(l);        char[] c = s.toCharArray();        int j = c.len…
求1+2!+3!+...+20!的和. public class Example21 {    public static void main(String[] args) {        sum(20);    } public static void sum(int n) {        long sum = 0;        long fac = 1;        for (int i = 1; i <= n; i++) {            fac *= i;        …
判断101-200之间有多少个素数,并输出所有素数. public class Example02 {    public static void main(String[] args) {        prime();    } public static void prime() {        int count = 0;        for (int i = 101; i < 2000; i += 2) {            boolean flag = false;     …
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件 "stud"中. public class Example50 {    public static void main(String[] args) {        stud();    } public static void stud() {        Scanner ss = new Scanner(System.in); …
企业发放的奖金根据利润提成:利润(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,求应发放奖金总数? public class Example12 {  …
809*??=800*??+9*??+1,其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数.求??代表的两位数,以及809*??后的结果. public class Example42 {    public static void main(String[] args) {        f();    } public static void f() {        int a = 809, b, i;        for (i = 10; i < 13; i++)…
取一个整数a从右端开始的4-7位. public class Example32 {    public static void main(String[] args) {        cut(123456789);    } public static void cut(long n) {        String s = Long.toString(n);        char[] c = s.toCharArray();        int j = c.length;      …
读取7个数(1~50)的整数值,每读取一个值,程序打印出该值个数的*. public class Example47 {    public static void main(String[] args) {        int[] a = { 10, 7, 6, 15, 4, 3, 20 };        display(a);    } public static void display(int[] a) {        System.out.print("读取的整数有:"…
编程实现两个字符串的连接. public class Example46 {    public static void main(String[] args) {        addString("hello"," world!");    }    public static void addString(String s1,String s2){        String s3=s1+s2;        System.out.println("…
将几个字符串排序(按英文字母的顺序). public class Example40 {    public static void main(String[] args) {        String[] s={"math","english","java","java web","rose"};        stringSort(s);    } public static void stringS…
一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. public class Example09 {    public static void main(String[] args) {        number();    } public static void number() {        int count = 0;        for (int i = 1; i <= 1000; i++) {     …
利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. public class Example05 { public static void main(String[] args) {        score(90);    } public static void score(int n) {        char s = n >= 90 ? 'A' : (n < 60 ? 'C' : 'B');        Sys…
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? public class Example01 {    public static void main(String[] args) {        int a = 8;        int sum = f(a);        System.out.println("第" + a + "个月的兔子数为:" + sum);    …
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字.例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制. public class Example08 {    public static void main(String[] args) {        sum(2, 5);    } public static void sum(int a, int n) {        int b = a;        long sum = 0; …
两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人,以抽签决定比赛名单.有人向队员打听比赛的名单:a说他不和x比,c说他不和x. z比.请编程序找出三队赛手的名单. public class Example18 {    public static void main(String[] args) {        vs();    } public static void vs() {        char[] m = { 'a', 'b', 'c' };      …
打印出杨辉三角形(要求打印出10行如下图)11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 1 public class Example33 { public static void main(String[] args) {        yanghui(10);    } public static void yanghui(int n) {        int[][] a = new int[n][n];        for (int i = 0; i < n;…
有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数. public class Example36 {    public static void main(String[] args) {        int[] m = { 18, 12, 23, 34, 95, 76, 57, 28, 9 };        moveElement(m, 5);    } public static void moveElement(int[] m, int n) {        …
海滩上有若干个一堆桃子,五只猴子来分.第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份. 第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份.第三.第四.第五只猴子都是这样做的.问海滩上原来最少有多少个桃子? public class Example41 {    public static void main(String[] args) {        number();    } public static void…
计算某字符串中子串出现的次数. public class Example49 {    public static void main(String[] args) {        String s1 = "adcdcjncfb";        String s2 = "";        count(s1, s2);    } public static void count(String str1, String str2) {        int cou…
某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下: 每位数字都加上5,然后用和除以10的余数代替该数字, 再将第一位和第四位交换,第二位和第三位交换. public class Example48 {    public static void main(String[] args) {        f(2345);    } public static void f(int num) {        int[] c = new int[4];      …
判断一个整数能被几个9整除. public class Example45 {    public static void main(String[] args) {        f(729);    } public static void f(int n) {        int tmp = n;        int count = 0;        for (int i = 0; tmp % 9 == 0;) {            tmp = tmp / 9;         …
求0~7所能组成的奇数个数.分析:组成1位数是4个,组成2位数是7*4个,组成3位数是7*8*4个,组成4位数是7*8*8*4个…… public class Example44 {    public static void main(String[] args) {        f();    } public static void f() {        int sum = 4;        int j;        System.out.println("组成1位数是 &quo…