//導(dǎo)入java.util.Arrays;import java.util.Arrays;public class HelloWorld {? ? public static void main(String[] args) {? ? ? ??? ? ? ? ?// 創(chuàng)建對象,對象名為hello? ? HelloWorld hello = new HelloWorld();? ? ? ??? ? ? ? // 調(diào)用方法并將返回值保存在變量中 int[] nums = hello.getArray(8);? ? ? ??? ? ? ? // 將數(shù)組轉(zhuǎn)換為字符串并輸出 System.out.println(Arrays.toString(nums));? } /* * 功能:創(chuàng)建指定長度的int型數(shù)組,并生成100以內(nèi)隨機數(shù)為數(shù)組中的每個元素賦值 * 定義一個帶參帶返回值的方法,通過參數(shù)傳入數(shù)組的長度,返回賦值后的數(shù)組 */ public int[] getArray(int length) {? ? ? ? // 定義指定長度的整型數(shù)組 int[] nums = new int[length];? ? ? ??? ? ? ? // 循環(huán)遍歷數(shù)組賦值 for (int i=0;i < nums.length ; i++){? ? ? ? ? ?? // 產(chǎn)生一個100以內(nèi)的隨機數(shù),并賦值給數(shù)組的每個成員 ? ?nums[i] = (int) (Math.randrom()*100);? ? ? ?? } return nums; // 返回賦值后的數(shù)組 }}
添加回答
舉報
0/150
提交
取消