Java初学之十个小程序实例

1.购买了一台彩电,价格是2856元,求:支付的纸币各多少张?

public class Demo01 {

public static void main(String[] args) {

int play = 2856;

System.out.println(“100元面值:” + play / 100);

System.out.println(“50元面值:” + play / 10 % 10 / 5);

System.out.println(“20元面值:” + play / 10 % 10 % 5 / 2);

System.out.println(“10元面值:” + play / 10 % 10 % 5 % 2);

System.out.println(“5元面值:” + play % 10 / 5);

System.out.println(“1元面值:” + play % 10 % 5);

}

}

2.模拟打怪过程:游戏总共20级,有英雄和怪兽两个角色,在第一级时,怪兽经验20点,升级所需经验1200点,英雄经验为0,英雄每杀一个怪,自身就增加怪兽的经验值;每升一级,升级所需经验涨20%(忽略小数部分),怪兽经验涨5%,英雄经验归0,求升到20级时,总共杀了多少怪。

public class Demo02 {

public static void main(String[] args) {

int monster = 20;

int count = 0;

int exp = 1200;

for (int d = 2; d <= 20; d++) { int hero = 0; monster = (int) (monster * 1.05); exp = (int) (exp * 1.2); while (hero < exp) { hero += monster; count++; } } System.out.println(count + 1200 / 20); } } 3. 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。 程序分析:采取逆向思维的方法,从后往前推断。 public class Demo03 { public static void main(String[] args) { int sum = 1; for(int i = 9;i > 0;i–){

sum = (sum + 1) * 2;

}

System.out.println(“第一天,有桃子”+sum+”颗”);

}

}

4.题目:输入某年某月某日,判断这一天是这一年的第几天?

程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

public class Demo04 {

public static void main(String[] args) {

int sum = 0;

Scanner sc = new Scanner(System.in);

System.out.println(“请输入几年:”);

int year = sc.nextInt();

System.out.println(“请输入几月:”);

int month = sc.nextInt();

System.out.println(“请输入几号:”);

int day = sc.nextInt();

GregorianCalendar gre = new GregorianCalendar();

boolean isLeapYear = gre.isLeapYear(year);

int ap = isLeapYear ? 29 : 28;

int days = 0;

switch (month) {

case 1:

days = day;

break;

case 2:

days = 31 + day;

break;

case 3:

days = 31 + ap + day;

break;

case 4:

days = 31 + ap + 31 + day;

break;

case 5:

days = 31 + ap + 31 + 30 + day;

break;

case 6:

days = 31 + ap + 31 + 30 + 31 + day;

break;

case 7:

days = 31 + ap + 31 + 30 + 31 + 30 + day;

break;

case 8:

days = 31 + ap + 31 + 30 + 31 + 30 + 31 + day;

break;

case 9:

days = 31 + ap + 31 + 30 + 31 + 30 + 31 + 31 + day;

break;

case 10:

days = 31 + ap + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;

break;

case 11:

days = 31 + ap + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;

break;

case 12:

days = 31 + ap + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;

break;

default:

System.out.println(“月份输入错误”);

break;

}

System.out.println(“这一天是这一年的第” + days + “天”);

}

}

5.题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。

public class Demo05 {

public static void main(String[] args) {

int count = 0;

for (int i = 1; i < 5; i++) { for (int j = 1; j < 5; j++) { for (int k = 1; k < 5; k++) { if (i != j && i != k && k != j) { count++; System.out.println(i * 100 + j * 10 + k); } } } } System.out.println("能组成" + count + "个互不相同无重复数字的三位数。"); } } 6.题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? public class Demo06 { public static void main(String[] args) { double height= 100; double sum=100; for(int i=0;i<10;i++) { height=height/2; sum+=height*2; } System.out.println("第十次反弹高度为:"+height+"米"); System.out.println("共经过:"+(sum-height*2)+"米"); } } 7.题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。 public class Demo07 { public static void main(String[] args) { System.out.println("100-999以内的水仙花数:"); for(int n=100;n<=999;n++) { int a; int b; int c; a=n%10; b=((n%100)-a)/10; c=(int) Math.floor(n/100); if(n==(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3))) { System.out.println(n); } } } } 8.题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 (2)如果n>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数n,重复执行第一步。

(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

public class Demo08 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

System.out.print(n + “=1*”);

for (int k = 2; k <= n / 2; k++) { if (n % k == 0) { System.out.print(k + "*"); n = n / k; k = 2; } } System.out.println(n); } } 9.题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

程序分析:(a>b)?a:b这是条件运算符的基本例子。

public class Demo09 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int a = sc.nextInt();

int n = sc.nextInt();

long s = 0l;

for (int i = 1; i <= n; i++) { s = s + a; a = a * 10 + a; } System.out.println(s); } } 10.题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。 public class Demo10 { public static void main(String[] args) { for (int i = 1; i < 1000; i++) { int sum = 0; for (int j = i - 1; j >= 1; j–) {

if (i % j == 0) {

sum += j;

}

}

if (sum == i) {

System.out.println(i);

}

}

}

}

发表评论