初级
1.定义一个函数,获取某个数组中的最小值
package practise;
public class GetMin {
public static void getMin(int[]num){
int min = num[0];
for(int i=0;i
min=(num[i]
}
System.*out*.print(min);
}
public static void main(String[] args){
int []a=new int[]{8,6,4,2,5};
*getMin*(a);
}
}
2.定义一个数组,数组成员10个,找出数组中最大数连同下标一起输出
package practise;
public class GetmaxIndex {
public static void main(String[] args) {
int[] n = new int[]{1, 2, 3, 4, 5, 6, 0};
int max = n[0];
int index=0;
for (int i = 0; i < n.length;i++){
if(max
max=n[i];
index=i;
}
}
System.*out*.println(max);
System.*out*.println(index);
}
}
3.给定一个整型数组,数组成员10个,求该数组中第二大的数的下标
package practise;
import java.util.Arrays;
public class TheSecendmax3 {
public static void main(String[] args) {
int[] num = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int[] arr1 = Arrays.*copyOf*(num, num.length);
Arrays.*sort*(num);
int[] num1 = Arrays.*copyOfRange*(num, 0, 9);
System.*out*.println(num1[num1.length-1]);
for (int i = 0; i < arr1.length-1; i++) {
if (arr1[i] == num1[num1.length-1]) {
System.*out*.println("第二大的数的下标为"+i);
}
}
}
}
4.B哥去参加青年歌手大奖赛,有10个评委打分,(去掉一个最高一个最低)求平均分?
package practise;
import java.util.Arrays;
public class AverageScore {
public static void main(String[] args){
double[] num=new double[]{99,97,96,95,94,92,91,90,88,100};
Arrays.*sort*(num);
double[] NewNum=Arrays.*copyOfRange*(num,1,9);
double average=0;
double sum=0;
for(int I=0;i
sum=sum+NewNum[I];
}
average=sum/NewNum.length;
System.*out*.println(average);
}
}
5.利用选择排序对数据进行降序排序
package practise;
public class Select_Sort {
//原理:在一组数组里面定义一个最大数值的下标,让int[index]和其它数值挨个比较,选出最大的值int[i],
// 并把该最大值的下标i和我们原先设置的index进行置换,即index=i;
//并且把他们两个的位置进行互换,依次类推,直到完全排序好
public static void main(String[] args){
int[] num=new int[]{4,3,1,6,9,5};
*ChooseSort*(num);
}
public static void ChooseSort(int[] array){
for(int I=0;i
Int index=i;//定义当前下标为最小值下标
for(int j=I+1;j
if(array[j]
index =j;
}
}
int temp = array[index];//交换
array[index] = array[I];
array[I] = temp;
}
for (int e:array){
System.*out*.print(e+”\t”);
}
}
}
6.定义数组,存放5个学生的成绩【成绩值自己设定】,将成绩从大到小排序,获得成绩之和,平均成绩,最小成绩,最大成绩。
package practise;
import java.util.Arrays;
public class StudentScore {
public static void Sort(double[] num) {
Arrays.*sort*(num);
double sum=0.0;
for (int e=0;e
sum=sum+num[e];
}
System.*out*.println(“max=“+num[num.length-1]);
System.*out*.println(“min=“+num[0]);
System.*out*.println(“average=“+sum/num.length);
System.*out*.println(“sum=“+sum);
}
public static void main(String[] agrs){
double[] num=new double[]{99,97,96,95,94,92,91,90,88,100};
*Sort*(num);
}
}
7.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
package practise;
import java.util.Arrays;
public class TheSeven {
public static void main(String[] args){
int[] num= new int[]{99,97,96,95,94,92,91,90,88,100};
Arrays.*sort*(num);
int evensum=0;
int oddsun=0;
for(int I=0;i
if(num[I]%2==0){
evensum++;
}else if(num[I]%3==0){
oddsun++;
}
}
System.*out*.println(“max=“+num[0]+”,min=“+num[num.length-1]);
System.*out*.println(“奇数的个数=“+oddsun+”偶数的个数=“+evensum);
}
}
8.提取一个方法,将指定数组中的数组元素进行反转
例如:{10,23,2,45,6}—>{6,45,2,23,10}
package practise;
public class BackOverNumberlist {
public static void main(String[] args){
int[] array=new int[]{1,2,3,4,5,6,7,8};
*BackOver*(array);
}
public static void BackOver(int[] array){
for(int i=array.length-1;i>=0;i--){
System.*out*.print(array[i]+" ");
}
}
}
扩展练习
输出100到1000的水仙花数:
public class LoopTest{
public static void main(String[] args){
int bai = 0;
int shi = 0;
int ge = 0;
for(int i = 100; i < 1000; i++){
bai = i/100;
shi = i/10%10;
ge = i%10;
if(bai*bai*bai+shi*shi*shi+ge*ge*ge == i ){
System.out.println(i);
}
}
}
}
打印所有字母:
public class LoopTest{
public static void main(String[] args){
char lower = 'a';
char upper = 'A';
for(int i = 0 ; i < 26 ; i++){
System.out.println(lower+" "+upper);
lower++;
upper++;
}
}
}