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");
}
}
}