Home  >  Article  >  Java  >  Ten small program examples for beginners to Java

Ten small program examples for beginners to Java

无忌哥哥
无忌哥哥Original
2018-07-23 10:17:3514945browse

1. I bought a color TV for 2856 yuan. Please ask: How many banknotes did you pay?

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. Simulate the process of defeating monsters: The game has a total of 20 levels, with two roles: heroes and monsters. At the first level, monster experience points are 20 points, upgrading requires 1200 experience points, and hero experience points are required. is 0. Every time the hero kills a monster, the monster's experience value increases; for every level up, the experience required for upgrading increases by 20% (ignoring the decimal part). The monster's experience increases by 5%, and the hero's experience returns to 0. Please increase it to 20 How many monsters have you killed in total?

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. Topic: Monkey eating peaches Problem: The monkey picked a few peaches on the first day, ate half of them immediately, and was still not satisfied. He ate one more peach and picked up the remaining peaches the next morning. I ate half and one more. From then on, I ate half and one of the leftovers from the previous day every morning. When I wanted to eat more on the morning of the 10th day, I saw that there was only one peach left. Find out how many were picked on the first day.

Program analysis: Adopt reverse thinking method and infer from back to front.

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. Question: Enter a certain year, a certain month and a certain day, and determine what day of the year this day is?

Program analysis: Taking March 5th as an example, you should add up the first two months, and then add 5 days, which is the day of the year. In special circumstances, it is a leap year and the input month is greater than Consider adding an extra day at 3 o'clock.

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. 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 places are all 1, 2, 3, and 4. After composing all the permutations, remove the permutations that do not meet the conditions.

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. Question: A ball falls freely from a height of 100 meters. Each time it hits the ground, it bounces back to half of its original height. If it falls again, how many meters does it pass by when it hits the ground for the 10th time? How high is the 10th rally?

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. Question: Print out all the "narcissus numbers". The so-called "narcissus number" 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 cubed + 5 cubed + 3 cubed.

Program analysis: Use a for loop to control 100-999 numbers, and decompose each number into units, tens, and hundreds.

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. Question: Decompose a positive integer into prime factors. For example: input 90, print out 90=2*3*3*5.

Program analysis: To decompose n into prime factors, you should first find a minimum prime number k, and then complete it according to the following steps:

(1) If this prime number is exactly equal to n, then it means The process of decomposing prime factors is over, just print it out.

(2) If n>k, but n is divisible by k, the value of k should be printed out, and the quotient of n divided by k should be used as the new positive integer n, and the first step should be repeated.

(3) If n is not divisible by k, use k 1 as the value of k and repeat the first step.

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. Question: Use the nesting of conditional operators to complete this question: students with academic scores >= 90 points are represented by A, students with scores between 60-89 are represented by B, and students with scores below 60 are represented by is represented by C.

Program analysis: (a>b)?a:b This is a basic example of conditional operator.

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. Question: If a number is exactly equal to the sum of its factors, the number is called a "perfect number". For example, 6=1+2+3. Program to find all perfect numbers within 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);	
			}
		}
	}
}

The above is the detailed content of Ten small program examples for beginners to Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn