共计 3857 个字符,预计需要花费 10 分钟才能阅读完成。
本文已同步至:村雨遥
实例 31
题目
将一个数组逆序输入。
剖析
输出多个元素存入数组,而后逆序遍历数组输入即可;
实现
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* Created with IntelliJ IDEA. | |
* | |
* @author : cunyu | |
* @version : 1.0 | |
* @email : 747731461@qq.com | |
* @website : https://cunyu1943.github.io | |
* @date : 2021/6/4 13:51 | |
* @project : Java 编程实例 | |
* @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("输出第" + (i + 1) + "个元素!"); | |
arr[i] = scanner.nextInt();} | |
System.out.println("输出的数组为:" + Arrays.toString(arr)); | |
System.out.println("逆序输入的数组"); | |
for (int i = arr.length - 1; i >= 0; i--) {System.out.print(arr[i] + "\t"); | |
} | |
} | |
} |
后果
实例 32
题目
取一个整数 a 从右端开始的 4 ~ 7 位。
剖析
输出一个整数,而后将其转换为字符串,而后遍历输入从右端开始的 4 ~ 7 位即可!
实现
import java.util.Scanner; | |
/** | |
* Created with IntelliJ IDEA. | |
* | |
* @author : cunyu | |
* @version : 1.0 | |
* @email : 747731461@qq.com | |
* @website : https://cunyu1943.github.io | |
* @date : 2021/6/4 13:56 | |
* @project : Java 编程实例 | |
* @package : PACKAGE_NAME | |
* @className : Example32 | |
* @description : | |
*/ | |
public class Example32 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in); | |
System.out.println("输出一个整数"); | |
int num = scanner.nextInt(); | |
String str = Integer.toString(num); | |
System.out.println("整数从右端开始的 4 ~ 7 位:"); | |
for (int i = str.length() - 4; i >= str.length() - 7; i--) {System.out.print(str.charAt(i)); | |
} | |
} | |
} |
后果
实例 33
题目
打印杨辉三角。
剖析
仔细观察杨慧三角能够看到:
第 0 列和对角线上的数据全副为 1,其余地位上的数据为上一行正对数据与上一行正对前一个数据之和。
比方:a[4][2] = a[3][2] + a[3][1]
实现
import java.util.Scanner; | |
/** | |
* Created with IntelliJ IDEA. | |
* | |
* @author : cunyu | |
* @version : 1.0 | |
* @email : 747731461@qq.com | |
* @website : https://cunyu1943.github.io | |
* @date : 2021/6/4 14:01 | |
* @project : Java 编程实例 | |
* @package : PACKAGE_NAME | |
* @className : Example33 | |
* @description : | |
*/ | |
public class Example33 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in); | |
System.out.println("输出杨辉三角的行数"); | |
int row = scanner.nextInt(); | |
int[][] triangle = new int[row][]; | |
System.out.println("杨辉三角为:"); | |
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();} | |
} | |
} |
后果
实例 34
题目
输出 3 个数 a、b、c,按大小程序输入。
剖析
输出 3 个数存入数组中,而后利用 Arrays.sort()
进行排序,最初依照从小到到输入即可。
实现
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* Created with IntelliJ IDEA. | |
* | |
* @author : cunyu | |
* @version : 1.0 | |
* @email : 747731461@qq.com | |
* @website : https://cunyu1943.github.io | |
* @date : 2021/6/4 14:06 | |
* @project : Java 编程实例 | |
* @package : PACKAGE_NAME | |
* @className : Example34 | |
* @description : | |
*/ | |
public class Example34 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in); | |
System.out.println("输出三个整数"); | |
int[] arr = new int[3]; | |
for (int i = 0; i < 3; i++) {System.out.println("输出第" + (i + 1) + "个整数"); | |
arr[i] = scanner.nextInt();} | |
Arrays.sort(arr); | |
System.out.println("从小到大的输入程序:"); | |
for (int i = 0; i < arr.length; i++) {if (i != arr.length - 1) {System.out.print(arr[i] + "<"); | |
} else {System.out.print(arr[i]); | |
} | |
} | |
} | |
} |
后果
实例 35
题目
输出数组,最大的与第一个元素替换,最小的与最初一个元素替换,输入数组。
剖析
将数组复制到新的数组中,而后找到最大的元素和最小元素的索引,而后再到原数组中替换最大元素和第一个元素,最小元素和最初一个元素。
实现
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* Created with IntelliJ IDEA. | |
* | |
* @author : cunyu | |
* @version : 1.0 | |
* @email : 747731461@qq.com | |
* @website : https://cunyu1943.github.io | |
* @date : 2021/6/4 14:15 | |
* @project : Java 编程实例 | |
* @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("输出第" + (i + 1) + "个元素"); | |
arr[i] = scanner.nextInt();} | |
int[] newArr = Arrays.copyOf(arr, arr.length); | |
Arrays.sort(newArr); | |
System.out.println("输出的数组:" + Arrays.toString(arr)); | |
// 找到最大元素和最小元素的索引地位 | |
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;} | |
} | |
// 替换最大元素和第一个元素 | |
int tmp1 = arr[0]; | |
arr[0] = arr[maxIndex]; | |
arr[maxIndex] = tmp1; | |
// 替换最小元素和最初一个元素 | |
int tmp2 = arr[arr.length - 1]; | |
arr[arr.length - 1] = arr[minIndex]; | |
arr[minIndex] = tmp2; | |
System.out.println("替换后的数组:" + Arrays.toString(arr)); | |
} | |
} |
后果
正文完