查找
package com.itheima;
import java.util.Scanner;
public class ninety_six {
public static void main(String[] args) {
System.out.println(“请输入一个数字:”);
Scanner sc = new Scanner (System.in);
int a = sc.nextInt();
int dex = -1;
int[] arr = {19,28,37,46,50};
for(int i=0;i<arr.length;i++){
if(a == arr[i]) {
dex = i;
}
}
System.out.println(dex);
}
}
用方法:
package com.itheima;
import java.util.Scanner;
public class ninety_six {
public static void main(String[] args) {
System.out.println(“请输入一个数字:”);
Scanner sc = new Scanner (System.in);
int a = sc.nextInt();
System.out.println(getNumber(a));
/* int dex = -1;
int[] arr = {19,28,37,46,50};
for(int i=0;i<arr.length;i++){
if(a == arr[i]) {
dex = i;
}
}
System.out.println(dex);*/
}
public static int getNumber(int a){
int[] arr = {19,28,37,46,50};
int index = -1;
for(int i=0;i<arr.length;i++){
if(a == arr[i]){
index = i;
break;
}
}
return index;
}
}
反转
package com.itheima;
public class niniety_seven {
public static void main(String[] args) {
int[] arr = {19,28,37,46,50};
int[] brr = {50,46,37,28,19};
System.out.print("arr=");
appear(arr);
System.out.print("brr=");
appear(brr);
reverse(arr,brr);
System.out.println("交换后的结果为:");
System.out.print("arr=");
appear(arr);
System.out.print("brr=");
appear(brr);
}
public static void reverse(int[] arr,int[] brr){
int temp;
for(int i=0;i<arr.length;i++){
temp = arr[i];
arr[i] = brr[i];
brr[i] = temp;
}
}
public static void appear(int[] arr){
System.out.print("{");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+",");
}
System.out.println("}");
}
}
评委打分
package com.itheima;
import java.util.Scanner;
public class ninety_eight {
public static void main(String[] args) {
int[] arr = new int[6];
for(int i=0;i<arr.length;i++){
Scanner sc = new Scanner(System.in);
System.out.println(“请输入第”+ (i+1) +“评委的评分:”);
arr[i] = sc.nextInt();
}
appear(arr);
int max = maxScore(arr);
int min = minScore(arr);
int sum = sum(arr);
System.out.println(“平均分为:”+ (sum-min-max)/(arr.length-2));
}
public static void appear(int[] arr){
System.out.print("得分为:{");
for(int i=0;i<arr.length;i++){
if(i == arr.length-1){
System.out.print(arr[i]);
}else{
System.out.print(arr[i] + ",");
}
}
System.out.println("}");
}
public static int maxScore(int[] arr){
int max = arr[0];
for(int i=1;i<arr.length;i++){
if(max<arr[i]){
max = arr[i];
}
}
return max;
}
public static int minScore(int[] arr){
int min = arr[0];
for(int i=1;i<arr.length;i++){
if(min>arr[i]){
min = arr[i];
}
}
return min;
}
public static int sum(int[] arr){
int sum = 0;
for(int i=0;i<arr.length;i++){
sum += arr[i];
}
return sum;
}
}