This is the 9th day of my participation in Gwen Challenge
This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details
Examples of 31
The title
Prints an array in reverse order.
Analysis of the
Input multiple elements into the array, and then reverse through the array output can be;
implementation
import java.util.Arrays;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : [email protected]
* @website : https://cunyu1943.github.io
* @date: 2021/6/4 they *@project: Java programming instance *@package : PACKAGE_NAME
* @className : Example31
* @description: * /
public class Example31 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("Input control" + (i + 1) + "An element!");
arr[i] = scanner.nextInt();
}
System.out.println("The input array is:" + Arrays.toString(arr));
System.out.println("Array of output in reverse order");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + "\t"); }}}Copy the code
The results of
Examples of 32
The title
The value is an integer from 4 to 7 digits starting from the right end of a.
Analysis of the
Enter an integer, convert it to a string, and iterate through the output 4 to 7 bits from the right end!
implementation
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : [email protected]
* @website : https://cunyu1943.github.io
* @date : 2021/6/4 13:56
* @project: Java programming instance *@package : PACKAGE_NAME
* @className : Example32
* @description: * /
public class Example32 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an integer");
int num = scanner.nextInt();
String str = Integer.toString(num);
System.out.println("Integer from 4 to 7 bits from the right end:");
for (int i = str.length() - 4; i >= str.length() - 7; i--) { System.out.print(str.charAt(i)); }}}Copy the code
The results of
Examples of 33
The title
Print Yang Hui triangle.
Analysis of the
A closer look at the Yanghui Triangle shows:
The data in column 0 and the diagonal are all 1’s, and the data in the remaining positions is the sum of the data in front of the previous row and the data in front of the previous row.
For example: A [4][2] = A [3][2] + a[3][1]
implementation
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : [email protected]
* @website : https://cunyu1943.github.io
* @date : 2021/6/4 14:01
* @project: Java programming instance *@package : PACKAGE_NAME
* @className : Example33
* @description: * /
public class Example33 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Number of rows input to Yanghui triangle");
int row = scanner.nextInt();
int[][] triangle = new int[row][];
System.out.println("Yang Hui triangle is:");
for (int i = 0; i < triangle.length; i++) {
triangle[i] = new int[i + 1];
for (int j = 0; j < triangle[i].length; j++) {
if (i == 0 || j == 0 || i == j) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i - 1][j] + triangle[i - 1][j - 1];
}
System.out.print(triangle[i][j] + ""); } System.out.println(); }}}Copy the code
The results of
Examples of 34
The title
Enter three numbers a, B, and C and output them in order of size.
Analysis of the
Arrays.sort() is used to sort and output from small to small.
implementation
import java.util.Arrays;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : [email protected]
* @website : https://cunyu1943.github.io
* @date : 2021/6/4 14:06
* @project: Java programming instance *@package : PACKAGE_NAME
* @className : Example34
* @description: * /
public class Example34 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three integers");
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
System.out.println("Input control" + (i + 1) + "A whole number");
arr[i] = scanner.nextInt();
}
Arrays.sort(arr);
System.out.println("Output order from small to large:");
for (int i = 0; i < arr.length; i++) {
if(i ! = arr.length -1) {
System.out.print(arr[i] + "<");
} else{ System.out.print(arr[i]); }}}}Copy the code
The results of
Examples of 35
The title
Input arrays, the largest swapped with the first element, the smallest swapped with the last element, output arrays.
Analysis of the
Copy the array into the new array, find the largest element and the index of the smallest element, and then swap the largest element with the first element, and the smallest element with the last element in the original array.
implementation
import java.util.Arrays;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
*
* @author : cunyu
* @version : 1.0
* @email : [email protected]
* @website : https://cunyu1943.github.io
* @date: "2021/6/4 *@project: Java programming instance *@package : PACKAGE_NAME
* @className : Example35
* @description: * /
public class Example35 {
public static int SIZE = 10;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
System.out.println("Input control" + (i + 1) + "The element");
arr[i] = scanner.nextInt();
}
int[] newArr = Arrays.copyOf(arr, arr.length);
Arrays.sort(newArr);
System.out.println("Input array:" + Arrays.toString(arr));
// Find the index positions of the largest and smallest elements
int minIndex = 0;
int maxIndex = 0;
for (int i = 0; i < SIZE; i++) {
if (arr[i] == newArr[0]) {
minIndex = i;
}
if (arr[i] == newArr[SIZE - 1]) { maxIndex = i; }}Swap the largest element with the first element
int tmp1 = arr[0];
arr[0] = arr[maxIndex];
arr[maxIndex] = tmp1;
Swap the smallest element with the last element
int tmp2 = arr[arr.length - 1];
arr[arr.length - 1] = arr[minIndex];
arr[minIndex] = tmp2;
System.out.println("Swapped array:"+ Arrays.toString(arr)); }}Copy the code