日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

实验2 java_《Java程序设计》实验2

發布時間:2023/12/4 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实验2 java_《Java程序设计》实验2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

}

}

}

總結

以上是生活随笔為你收集整理的实验2 java_《Java程序设计》实验2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。