[java]输入一个整型数组,然后求出该数组中最大的元素与最小的元素,最后逆序输出该数组。怎么编程
发布网友
发布时间:2022-04-29 15:39
我来回答
共2个回答
热心网友
时间:2023-10-16 05:35
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]);
}
}
}
热心网友
时间:2023-10-16 05:36
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] + " ");
}
}
}
热心网友
时间:2023-10-16 05:36
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]);
}
}
}
热心网友
时间:2023-10-16 05:36
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] + " ");
}
}
}
热心网友
时间:2023-10-16 05:36
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]);
}
}
}
热心网友
时间:2023-10-16 05:36
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] + " ");
}
}
}