JAVA基础(代码)练习题91~120

java基础

 

91.设计一个方法求出任意int数组中第二小的值

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组中第二小的值
 * @author Administrator
 *
 */
public class Homework91 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        
        
        return arr[arr.length-2];
    }
}
View Code

 

92.设计一个方法求出任意int数组所有元素的平均值

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组所有元素的平均值
 * @author Administrator
 *
 */
public class Homework92 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            sum = sum +arr[i];
        }
        avr = sum / arr.length;
        
        return avr;
    }
}
View Code

 

93.设计一个方法求出任意int数组奇数的个数

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组奇数的个数
 * @author Administrator
 *
 */
public class Homework93 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 2 != 0) {
                count++;
            }
        }
    
        
        return count;
    }
}
View Code

 

94.设计一个方法求出任意int数组奇数的和

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组奇数的和
 * @author Administrator
 *
 */
public class Homework94 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 2 != 0) {
                sum = sum + arr[i];
            }
        }
    
        
        return sum;
    }
}
View Code

 

95.设计一个方法求出任意int数组偶数的和

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组偶数的和
 * @author Administrator
 *
 */
public class Homework95 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        int sum = 0;
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 2 == 0) {
                sum = sum + arr[i];
            }
        }
    
        
        return sum;
    }
}
View Code

 

96.设计一个方法求出任意int数组偶数的个数

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * .设计一个方法求出任意int数组偶数的个数
 * @author Administrator
 *
 */
public class Homework96 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 2 == 0) {
                count++;
            }
        }
    
        
        return count;
    }
}
View Code

 

97.设计一个方法求出任意int数组3的倍数的个数

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * .设计一个方法求出任意int数组3的倍数的个数
 * @author Administrator
 *
 */
public class Homework97 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 3 == 0) {
                count++;
            }
        }
    
        
        return count;
    }
}
View Code

 

98.设计一个方法求出任意int数组3的倍数的和

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组3的倍数的和
 * @author Administrator
 *
 */
public class Homework98 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 3 == 0) {
                count = count + arr[i];
            }
        }
    
        
        return count;
    }
}
View Code

 

99.设计一个方法求出任意int数组7的倍数的和

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求出任意int数组7的倍数的和
 * @author Administrator
 *
 */
public class Homework99 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int count = 0;
        
        for (int i = 0; i < arr.length; i++) { 
            if (arr[i] % 7 == 0) {
                count = count + arr[i];
            }
        }
    
        
        return count;
    }
}
View Code

 

100.设计一个方法求出任意int数组7的倍数的个数

JAVA基础(代码)练习题91~120
package Homework_120;
/**
 * 设计一个方法求出任意int数组7的倍数的个数
 * @author Administrator
 *
 */
public class Homework100 {
    public static void main(String[] args) {
        int[] arr = {1, 3, 7, 8, 9, 4, 5, 6, 14, 21};
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 7 == 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}
View Code

 

101.设计一个方法判断一个数字是几位数

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法判断一个数字是几位数
 * @author Administrator
 *
 */
public class Homework101 {
    public static void main(String[] args) {
        /*
         * 思路:将数字转化为字符串,计算字符串的长度,从而得出数字的长度,也就是几位数
         */
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数字:");
        int num = scan.nextInt();
        String str = String.valueOf(num);
        guessNum(str);
        
    }
    public static void guessNum(String str) {
        
        System.out.println(str.length());
    }
}
View Code


102.设计一个方法将任意2个int数组合并

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法将任意2个int数组合并
 * @author Administrator
 *
 */
public class Homework102 {
    public static void main(String[] args) {
        // 输入第一个数组
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第一个数组,以逗号隔开");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        
        //输入第二个数组
        System.out.println("请输入第二个数组,以逗号隔开");
        String str1 = scan.next().toString();
        String[] arr1 = str1.split(",");
        int[] b1 = new int[arr1.length];
        for (int i = 0; i < b1.length; i++) {
            b1[i] = Integer.parseInt(arr1[i]);
        }
        int[] sum = getTwoArr(b, b1);
        for (int i : sum) {
            System.out.print(i + " ");
        }
        
        
    }
    public static int[] getTwoArr(int[] a, int[] b) {
        /*
         * 声明一个新数组,合并两个数组
         */
        int[] sum = new int[a.length+b.length];
        for (int i = 0; i < a.length; i++) {
            sum[i] = a[i];
        }
        for (int j = 0; j < b.length; j++) {
            sum[j+a.length] = b[j];
        }
        return sum;
    }
}
View Code

 

103.设计一个方法将任意int数组升序排序

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法将任意int数组升序排序
 * @author Administrator
 *
 */
public class Homework103 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        int[] a = getArr(num);
        for (int i : a) {
            System.out.println(i);
        }
        
    }
    public static int[] getArr(int[] arr) {
        /*
         * 实现传入一个数组,然后对数组升序排序
         */
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
            
        }
        return arr;
    }
}
View Code

 

104.设计一个方法将任意int数组降序排序

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法将任意int数组降序排序
 * @author Administrator
 *
 */
public class Homework104 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        int[] a = getArr(num);
        for (int i : a) {
            System.out.println(i);
        }
        
    }
    public static int[] getArr(int[] arr) {
        /*
         * 实现传入一个数组,然后对数组降序排序
         */
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
            
        }
        return arr;
    }
}
View Code

 

105.设计一个方法求任意int数组最大的数的下标

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最大的数的下标
 * @author Administrator
 *
 */
public class Homework105 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        int a = getMaxtArr(num);
        System.out.println(a);

        
    }
    public static int getMaxtArr(int[] arr) {
        int index = 0;
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
                index = i;
            }
            
        }
        return index;
    }
}
View Code

 

106.设计一个方法求任意int数组最小的数的下标

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最小的数的下标
 * @author Administrator
 *
 */
public class Homework106 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        int a = getMaxtArr(num);
        System.out.println(a);

        
    }
    public static int getMaxtArr(int[] arr) {
        int index = 0;
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
                index = i;
            }
            
        }
        return index;
    }
}
View Code

 

107.设计一个方法将数组中的元素反转

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法将数组中的元素反转
 * @author Administrator
 *
 */
public class Homework107 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        int[] a = getArr(num);
        for (int i : a) {
            System.out.println(i); 
        }
        
        
        
    }
    public static int[] getArr(int[] arr) {
        /*
         * 函数作用,数组翻转
         */
        for (int i = 0; i < arr.length / 2; i++) { // 原理,数组最前的元素与最后的元素互换,知道中间的元素,停止
            if (i < arr.length/2) {
                int temp = arr[arr.length-i-1];
                arr[arr.length-1] = arr[i];
                arr[i] = temp;
            }
        }
        return arr;
        
    }
}
View Code

 

108.设计一个方法将数组中的元素反转并放回原来的数组

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法将数组中的元素反转并放回原来的数组
 * @author Administrator
 *
 */
public class Homework108 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入int类型数组,每个数字后面逗号结尾:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] num = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            num[i] = Integer.parseInt(arr[i]); // 将字符串数组的每个元素转换为int类型的
        }
        num = getArr(num);
        for (int i : num) {
            System.out.println(i); 
        }
        
        
        
    }
    public static int[] getArr(int[] arr) {
        /*
         * 函数作用,数组翻转
         */
        for (int i = 0; i < arr.length / 2; i++) { // 原理,数组最前的元素与最后的元素互换,知道中间的元素,停止
            if (i < arr.length/2) {
                int temp = arr[arr.length-i-1];
                arr[arr.length-1] = arr[i];
                arr[i] = temp;
            }
        }
        return arr;
        
    }
}
View Code

 

109.设计一个方法求任意int数组第二小的数的下标

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组第二小的数的下标
 * @author Administrator
 *
 */
public class Homework109 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int index = 0;
        int min = arr[0];
        int[] arr1 = new int[arr.length];
        
        for (int i = 0; i < arr.length; i++) { // 将数组的值赋值给另一个数组
            arr1[i] = arr[i];
        }
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr1.length; i++) { //查询赋值数组中第二小的下标
            if (arr1[i] == arr[1]) {
                index = i;
            }
        }
        
        
        return index;
    }
}
View Code

 

110.设计一个方法求任意int数组第二大的数的下标

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组第二大的数的下标
 * @author Administrator
 *
 */
public class Homework110 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static int getArrayIndex(int[] arr) {
        int index = 0;
        int min = arr[0];
        int[] arr1 = new int[arr.length];
        
        for (int i = 0; i < arr.length; i++) { // 将数组的值赋值给另一个数组
            arr1[i] = arr[i];
        }
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr1.length; i++) { //查询赋值数组中第二大的下标
            if (arr1[i] == arr[1]) {
                index = i;
            }
        }
        
        
        return index;
    }
}
View Code

 

111.设计一个方法求任意int数组最大值最小值去掉求平均值

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最大值最小值去掉求平均值
 * @author Administrator
 *
 */
public class Homework111 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        int index = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {  // 除掉最大值和最小值,其他数求平均值
            if (i != 0 && i != arr.length - 1) {
                index = index + arr[i];
                System.out.println(index);
            }     
        }
        double avr = index / (arr.length - 2);
        return avr;
    }
}
View Code


112.设计一个方法求任意int数组最大值最小值的差

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最大值最小值的差
 * @author Administrator
 *
 */
public class Homework112 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        int index = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        index = arr[0] - arr[arr.length-1];
        
        return index;
    }
}
View Code

 

113.设计一个方法求任意int数组最大值和平均值的差

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最大值和平均值的差
 * @author Administrator
 *
 */
public class Homework113 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        avr = sum / arr.length;
        avr = arr[0] - avr;
        
        return avr;
    }
}
View Code

 

114.设计一个方法求任意int数组最小值和平均值的差

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组最大值和平均值的差
 * @author Administrator
 *
 */
public class Homework113 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        avr = sum / arr.length;
        avr = arr[0] - avr;
        
        return avr;
    }
}
View Code

 

115.设计一个方法求任意int数组第二大值和平均值的差

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组第二大值和平均值的差
 * @author Administrator
 *
 */
public class Homework115 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        avr = sum / arr.length;
        avr = arr[1] - avr;
        
        return avr;
    }
}
View Code

 

116.设计一个方法求任意int数组第二小值和平均值的差

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组第二小值和平均值的差
 * @author Administrator
 *
 */
public class Homework116 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        double index = getArrayIndex(b);
        System.out.println(index);
        
    }
    
    public static double getArrayIndex(int[] arr) {
        double avr = 0;
        int sum = 0;
        int min = arr[0];    
        for (int i = 0; i < arr.length; i++) { // 排序
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] < arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i];
        }
        avr = sum / arr.length;
        avr = arr[arr.length-2] - avr;
        
        return avr;
    }
}
View Code

 

117.设计一个方法求任意int数组偶数的积

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组偶数的积
 * @author Administrator
 *
 */
public class Homework117 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int mul = getMul(b);
        System.out.println(mul);
    }
    public static int getMul(int[] arr) {
        int mul = 1;
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 == 0) {
                mul= mul * arr[i];
            }
            
        }
        return mul;
    }
}
View Code

 

118.设计一个方法求任意int数组奇数的积

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组奇数的积
 * @author Administrator
 *
 */
public class Homework118 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int mul = getMul(b);
        System.out.println(mul);
    }
    public static int getMul(int[] arr) {
        int mul = 1;
        for (int i = 0; i < arr.length; i++) {
            if (i % 2 != 0) {
                mul= mul * arr[i];
            }
            
        }
        return mul;
    }
}
View Code

 

119.设计一个方法求任意int数组奇数比偶数多 多少个

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * .设计一个方法求任意int数组奇数比偶数多 多少个
 * @author Administrator
 *
 */
public class Homework119 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int mul = getMul(b);
        System.out.println("偶数比奇数多:" + mul);
    }
    public static int getMul(int[] arr) {
        int num1 = 0; // 偶数个数
        int num2 = 0; // 奇数个数
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            
            if (arr[i] % 2 != 0) {
                num2++;
            } else {
                num1++;
            }
            
        }
        count = num2 - num1;
        return count;
    }
}
View Code

 

120.设计一个方法求任意int数组奇数和比偶数和 多多少

JAVA基础(代码)练习题91~120
package Homework_120;

import java.util.Scanner;

/**
 * 设计一个方法求任意int数组奇数和比偶数和 多多少
 * @author Administrator
 *
 */
public class Homework120 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入数组,元素后接逗号:");
        String str = scan.next().toString();
        String[] arr = str.split(",");
        int[] b = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            b[i] = Integer.parseInt(arr[i]);
        }
        int mul = getMul(b);
        System.out.println("偶数比奇数多:" + mul);
    }
    public static int getMul(int[] arr) {
        int sum1 = 0; // 奇数和
        int sum2 = 0; // 偶数和
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            
            if (arr[i] % 2 != 0) {
                sum1 = sum1 + arr[i]; 
            } else {
                sum2 = sum2 + arr[i]; 
            }
            
        }
        count = sum1 - sum2;
        return count;
    }
}
View Code

 

上一篇:Centos7之ssh连接keepalive


下一篇:最长上升子LIS序列详解---nlogn算法+n^2算法求长度及序列本身+洛谷导弹拦截例题