import java.util.Scanner;
public class experiment5DemoTest {
public static void main(String[] args) {
MyArray s=new MyArray();//创建MyArray的对象
Scanner x=new Scanner(System.in); //创建一个键盘输入的值作为数组的大小
System.out.println("请给定一个数作为数组的大小");
int n=x.nextInt();//n为数组大小
int[] arr =new int[n];//创建一个大小为n的数组
for (int i=0;i<n;i++){ //通过for循环由由键盘一次给数组赋值
System.out.println(STR."请为数组第\{i}位赋值");
arr[i]=x.nextInt();
}
System.out.println("数组的遍历为");
s.follow(arr);
System.out.println("数组的倒数为");
s.palindromic(arr);
System.out.println("数组的最大值为");
s.max(arr);
}
}
class MyArray{
int max=0;
void follow(int[] arr){ //遍历数组的方法
for (int j : arr) {
System.out.println(j);
}
}
void palindromic(int[] arr){ //求数组倒叙的方法
for (int i=arr.length-1;i>=0;i--){
System.out.println(arr[i]);
}
}
void max(int[] arr){ //求数组中最大值的方法
for (int j : arr) {
max = arr[0];
if (j > max) {
max = j;
}
}
System.out.println(max);
}
}
运行结果:
请给定一个数作为数组的大小
6
请为数组第0位赋值
2
请为数组第1位赋值
3
请为数组第2位赋值
4
请为数组第3位赋值
5
请为数组第4位赋值
6
请为数组第5位赋值
7
数组的遍历为
2
3
4
5
6
7
数组的倒数为
7
6
5
4
3
2
数组的最大值为
7