好房网

网站首页常识百科 >正文

10个java编程案例

2022-06-28 04:42:41 常识百科来源:
导读相信目前很多小伙伴对于10个java编程案例都比较感兴趣,那么小搜今天在网上也是收集了一些与10个java编程案例相关的信息来分享给大家,希望...

相信目前很多小伙伴对于10个java编程案例都比较感兴趣,那么小搜今天在网上也是收集了一些与10个java编程案例相关的信息来分享给大家,希望能够帮助到大家哦。

1、题目:判断1-200之间有多少个素数,并输出所有素数。程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。public class lianxi02 {public static void main(String[] args) {int count = 0;for(int i=1; i<200; i+=2) {boolean b = false;for(int j=2; j<=Math.sqrt(i); j++){if(i % j == 0) { b = false; break; }else            { b = true; }}if(b == true) {count ++;System.out.println(i );}}System.out.println( "素数个数是: " + count);}}

2、题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 "水仙花数 ",因为153=1的三次方+5的三次方+3的三次方。public class lianxi03 {public static void main(String[] args) {int b1, b2, b3;for(int m=1; m<1000; m++) {b3 = m / 100;b2 = m % 100 / 10;b1 = m %    10;if((b3*b3*b3 + b2*b2*b2 + b1*b1*b1) == m) {System.out.println(m+"是一个水仙花数"); }}}}

3、题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。package WuYang;import java.util.*;public class lianxi07 {public static void main(String[] args) {int abcCount=0;//英文字母个数int spaceCount=0;//空格键个数int numCount=0;//数字个数int otherCount=0;//其他字符个数Scanner scan = new Scanner(System.in);//扫描器接受控制台的输入信息System.out.println("输入一组字符");String str=scan.nextLine();//取出控制台的一行信息,也就是你输入的信息char[] ch = str.toCharArray();//把取道的字符串变成一个char数组for(int i=0;i<ch.length;i++){if(Character.isLetter(ch[i])){//判断是否字母abcCount++;}else if(Character.isDigit(ch[i])){//判断是否数字numCount++;}else if(Character.isSpaceChar(ch[i])){//判断是否空格键spaceCount++;}else{//以上都不是则认为是其他字符otherCount++;}}System.out.println("字母个数:"+abcCount);System.out.println("数字个数:"+numCount);System.out.println("空格个数:"+spaceCount);System.out.println("其他字符个数:"+otherCount);}}

4、题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?public class lianxi10 {public static void main(String[] args) {double h = 100,s = 0;for(int i=1; i<=10; i++) {s = s + 2*h;h = h / 2;}s=s-100;System.out.println("经过路程:" + s);System.out.println("最后高度:" + h);}}

5、题目:有1、2、3、4四个数字,能组成多少个互不相同且一个数字中无重复数字的三位数?并把他们都输入。public class lianxi11 {public static void main(String[] args) {int count = 0;for(int x=1; x<5; x++) {for(int y=1; y<5; y++) {for(int z=1; z<5; z++) {if(x != y && y != z && x != z) {count ++;System.out.println(x*100 + y*10 + z );}}}}System.out.println("共有" + count + "个三位数");}}

6、题目:企业发放的奖金根据利润提成。利润(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%提成,从键盘输入当月利润,求应发放奖金总数?import java.util.*;public class lianxi12 {public static void main(String[] args) {double x = 0,y = 0;System.out.print("输入当月利润(万):");Scanner s = new Scanner(System.in);x = s.nextInt();if(x > 0 && x <= 10) {y = x * 0.1;} else if(x > 10 && x <= 20) {y = 10 * 0.1 + (x - 10) * 0.075;} else if(x > 20 && x <= 40) {y = 10 * 0.1 + 10 * 0.075 + (x - 20) * 0.05;} else if(x > 40 && x <= 60) {y = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (x - 40) * 0.03;} else if(x > 60 && x <= 100) {y = 20 * 0.175 + 20 * 0.05 + 20 * 0.03 + (x - 60) * 0.5;} else if(x > 100) {y = 20 * 0.175 + 40 * 0.08 + 40 * 0.5 + (x - 100) * 0.;}System.out.println("应该提取的奖金是 " + y + "万");}}

7、题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?public class lianxi13 {public static void main(String[] args) {for(int x =1; x<100000; x++) {if(Math.sqrt(x+100) % 1 == 0) {if(Math.sqrt(x+168) % 1 == 0) {System.out.println(x + "加100是一个完全平方数,再加168又是一个完全平方数");}}}}}

8、题目:输出9*9口诀。public class lianxi16 {public static void main(String[] args) {for(int i=1; i<10; i++) {for(int j=1; j<=i; j++) {System.out.print(j + "*" + i + "=" + j*i + "    " );if(j*i<10){System.out.print(" ");}}System.out.println();}}}

9、题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个     第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下     的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。public class lianxi17 {public static void main(String[] args) {int x = 1;for(int i=2; i<=10; i++) {x = (x+1)*2;}System.out.println("猴子第一天摘了 " + x + " 个桃子");}}

这是我学过的几个小案例

学java要有耐心

12、

本文到此结束,希望对大家有所帮助。


版权说明:本文由用户上传,如有侵权请联系删除!


标签:

热点推荐
热评文章
随机文章