//導入java.util.Arrays;
public?class?HelloWorld?{
????public?static?void?main(String[]?args)?{
????????
?????????//?創建對象,對象名為hello
???? HelloWorld?hello?=?new?HelloWorld();
????????
????????//?調用方法并將返回值保存在變量中
int[]?nums?=?hello.getArray(8);
????????
????????//?將數組轉換為字符串并輸出
System.out.println(Arrays.toString(nums));?
}
/*
?*?功能:創建指定長度的int型數組,并生成100以內隨機數為數組中的每個元素賦值
?*?定義一個帶參帶返回值的方法,通過參數傳入數組的長度,返回賦值后的數組
?*/
public?int[]?getArray(int?length)?{
????????//?定義指定長度的整型數組
int[]?nums?=?new?int[length];
????????
????????//?循環遍歷數組賦值
for?(??int?i=0;i<nums.length;i++??)?{
????????????
//?產生一個100以內的隨機數,并賦值給數組的每個成員
????nums[i]=(int)(Math.random()*100);
????????
}
return?nums;?//?返回賦值后的數組
}
}
代碼哪里出錯了啊,找不出來了!
lizaizhizhu
2017-04-21 10:41:33