Keyboard input month, output the corresponding season
case penetration:
The essence is that there is no break in the case statement body
If there is no break, it will directly enter the next case statement The body continues to execute until there is a break.
Scanner sc = System.out.println("请输入数字" num = 1 2 12 System.out.println("冬季" 3 4 5 System.out.println("c季" 6 7 8 System.out.println("夏季" 9 10 11 System.out.println("秋季" System.out.println("呵呵呵 " }
Prints all the palindromes in the 5-digit number
Range: 10000 ~ 100000
Conditions: Units place == thousands place && Tens place == thousands place
1 public static void main(String[] args) { 2 3 for(int i = 10000; i < 100000; i++){ 4 //求各个位 5 int g = i%10; 6 int s = i/10%10; 7 //int b = i/10/10%10; 8 int q = i/10/10/10%10; 9 int w = i/10/10/10/10%10;10 //条件判断 11 if(g == w && s == q){12 System.out.println(i);13 }14 }15 16 }
Array element exchange Fibonacci
Original array int[] arr = {10, 20,30,40,50};
Output arr={ 50, 40, 30, 20, 10};
Idea:
Define two variables i and j to represent the previous and following elements respectively The index value
Repeatedly swapping elements and moving the index value in the case of i 1 public static void main(String[] args) {
2 // TODO Auto-generated method stub
3 int[] arr = {10,20,30,40,50};
4 //定义两个变量 i j
5 int i = 0;
6 int j = arr.length-1;
7 //在 i< j的情况下反复交换和移动ij
8 while(i<j){
9 //交换
10 int tmp = arr[j]; // 把后面元素的值 存放到临时变量中
11 arr[j] = arr[i]; //把前面元素值 赋值给后面元素
12 arr[i] = tmp; // 把临时变量中存放的 原后面元素的值 赋值给前面的元素
13
14 //移动索引值
15 i++;
16 j--;
17 }
There is a pair of rabbits, From the 3rd month after birth, a pair of rabbits will be born every month. After the rabbit grows to the third month, another pair of rabbits will be born every month. If the rabbits do not die, ask about the 20th month. What is the number of pairs of rabbits? 1 public static void main(String[] args) {
2 // TODO Auto-generated method stub
3 int[] arr = new int[20];
4
5 arr[0] = 1;
6 arr[1] = 1;
7
8 for(int i = 2;i<arr.length;i++){
9 arr[i] = arr[i-1] + arr[i-2];
10 }
11
12 System.out.println("最终的兔子数: "+arr[19]);
13 }
The above is the detailed content of Write various types of code. For more information, please follow other related articles on the PHP Chinese website!