[java]输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该数组。怎么编程
[java]输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该数组。怎么编程输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该...
[java]输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该数组。怎么编程输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该数组。
展开
2个回答
展开全部
import java.util.Scanner;
public class Test {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
int count = 0;
int temp;
int max, min;
System.out.println("请输入数组的大小");
count = scanner.nextInt();
System.out.println("请输入数组:");
int[] numbers = new int[count];
for (int i = 0; i < count; i++)
numbers[i] = scanner.nextInt();
System.out.println("数组为:");
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);
max = min = numbers[0];
for (int i = 0; i < numbers.length; i++) {
if (max < numbers[i]) {
max = numbers[i];
}
if (min > numbers[i]) {
min = numbers[i];
}
}
System.out.println("最大数为:" + max + "最小数为:" + min);
for (int i = 0; i < numbers.length / 2; i++) {
temp = numbers[i];
numbers[i] = numbers[count - 1 - i];
numbers[count - 1 - i] = temp;
}
System.out.println("数组逆序为:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class HandleArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
HandleArray aa = new HandleArray();
}
public HandleArray() {
String str = "";
System.out.println("请输入一行整数(空格隔开):");
InputStreamReader input = new InputStreamReader(System.in);
try {
str = new BufferedReader(input).readLine();
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] array = str.split(" ");
int[] data = new int[array.length];
int max = 0;
int min = 0;
for (int i = 0; i < data.length; i++) {
int temp = Integer.parseInt(array[i]);
if (i == 0) {
max = temp;
min = temp;
}
if (temp > max)
max = temp;
if (temp < min)
min = temp;
int j;
for (j = 0; j <= i; j++) {
if (data[j] <= temp) {
for (int k = i; k > j; k--) {
data[k] = data[k - 1];
}
data[j] = temp;
break;
}
}
if (j > i)
data[j - 1] = temp;
}
System.out.println("最大值:" + max + ";最小值:" + min + "\n逆序输出:");
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询