1、使用java语言编程,从键盘输入N个整数存储到数组中,求数组所有元素的和、最大值和平均值。
import java.util.Scanner;
public class Program01{
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("qing shu ru shu zi ge shu: ");
int number = scanner.nextInt();
int[] scores = new int[number];
for(int i = 0;i
scores[i] = scanner.nextInt();
}
int maxScore=scores[0];
for(int i=0;i
if(maxScore
maxScore=scores[i];
}
}
System.out.println("zui da zhi: "+maxScore);
int minScore=scores[0];
for(int i=0;i
if(minScore>scores[i]){
minScore=scores[i];
}
}
System.out.println("zui xiao zhi: "+minScore);
int sum=0;int avg=0;
for(int i=0;i
sum+=scores[i];
}
avg=sum/scores.length;
System.out.println("pin jun zhi: "+avg);
}
}
2、使用java语言编程,产生一万个一到九之间的随机数,统计每个数出现的概率。
import java.util.Random;
public class Program02 {
public static void main(String[] args) {
int[] times = new int[9];
int n = 10000;
Random r= new Random();
for (int i = 0; i < n; i++) {
int num = r.nextInt(9)+1;
times[num - 1] = times[num - 1] + 1;
}
for (int i = 0; i < times.length; i++) {
System.out.println((i + 1) + "chu xian de ci shu: " + times[i]);
}
}
}
3、使用java语言编程,从键盘输入10个整数,并存放到一个数组中,然后将其前面五个元素与后面五个元素对换,即:第一个元素与第十个元素互换,第二个元素和第九个元素互换,... ...,第五个元素和第六个元素互换。分别输出数组原来各元素和互换后的各元素的值。
public class Program03{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int [] num=new int [10];
for(int i=0;i<10;i++){
num[i]=scanner.nextInt();
}
int [] num1=new int[num.length];
for(int i=0;i
num1[i]=num[i];
}
for(int i=0;i
int temp = num[i];
num[i]=num[num.length-i-1];
num[num.length-i-1]=temp;
}
for(int i=0;i
System.out.print(num1[i]+"\t");
}
System.out.print("\r\n");
for(int i=0;i
System.out.print(num[i]+"\t");
}
}
}