输出数组中最大值
public class ArrToBig {
public static void main(String[] args) {
int[] arr={-3,6,-3,-10,-5,-5,2};
boolean result=true;
int num=0;
int num2=arr[0];
for (int i=0;i<arr.length;i++){
if (i+1<arr.length){
if (num<arr[i]){
result=true;
num=arr[i];
}
if (num>arr[i]){
result=false;
if (num2<arr[i+1]) {
num2=arr[i+1];
}
}
}
}
if (result){
System.out.println("最大值为:"+num);
}else {
System.out.println("最大值为:"+num2);
}
}
}
输出数组中最大值下标
public class ArrToBigIndex {
public static void main(String[] args) {
int[] arr={-2,-1,-3,-10,-5,-5,-2};
boolean result=true;
int num=0;
int num2=arr[0];
int index=0;
for (int i=0;i<arr.length;i++){
if (i+1<arr.length){
if (num<arr[i]){
result=true;
num=arr[i];
index=i;
}
if (num>arr[i]){
result=false;
if (num2<arr[i+1]) {
num2=arr[i+1];
index=i+1;
}
}
}
}
if (result){
System.out.println("最大值下标为:"+index);
}else {
System.out.println("最大值下标为:"+index);
}
}
}
输出数组中最小值
public class ArrToNoBig {
public static void main(String[] args) {
int[] arr={-2,4,-3,-10,-15,-5,3};
boolean result=true;
int num=arr[0];
int num2=arr[0];
int index=0;
for (int i=0;i<arr.length;i++){
if (i+1<arr.length){
if (num>arr[i+1]){
result=true;
num=arr[i+1];
index=i+1;
}
if (num>arr[i]){
result=false;
if (num2>arr[i+1]) {
num2=arr[i+1];
index=i+1;
}
}
}
}
if (result){
System.out.println("最小值为:"+num);
}else {
System.out.println("最小值为:"+num2);
}
}
}
输出数组中最小值下标
public class ArrToNoBigIndex {
public static void main(String[] args) {
int[] arr={-2,4,-3,-10,-15,-5,3};
boolean result=true;
int num=arr[0];
int num2=arr[0];
int index=0;
for (int i=0;i<arr.length;i++){
if (i+1<arr.length){
if (num>arr[i+1]){
result=true;
num=arr[i+1];
index=i+1;
}
if (num>arr[i]){
result=false;
if (num2>arr[i+1]) {
num2=arr[i+1];
index=i+1;
}
}
}
}
if (result){
System.out.println("最小值下标为:"+index);
}else {
System.out.println("最小值下标为:"+index);
}
}
}