3 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
這是生成隨機(jī)整數(shù)的方法
public static void main(String[] args) {
int []count = new int[10];
Random r = new Random(7);
int x=0;
for (int i = 0; i < count.length; i++)
{
x = r.nextInt();
count[i] = x;
}
System.out.println("Max Number :"+maxNumber(count));}//Getting Max Number
以下是如何制作方法并從列表中獲取最大數(shù)量。
static int maxNumber(int[] mArray){//Passing int array as parameter
int max=mArray[0];
for(int i=0;i<mArray.length;i++){
if(max<mArray[i]){//Calculating max Number
max=mArray[i];
}
}
return max;//Return Max Number.
}
問有沒有什么不清楚的。這就是我們?nèi)绾沃谱鞣祷?int 的方法。

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
將最大值初始化為數(shù)組的第一個(gè)值。然后使用 for 循環(huán)迭代數(shù)組并使用最大值檢查數(shù)組當(dāng)前值。或者你可以sort
數(shù)組并返回。祝你好運(yùn)!

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
你的代碼應(yīng)該是這樣的。閱讀評(píng)論以了解它
public class Assignment {
public static int findMax(int[] arr) { // Defiine a function to find the largest integer in the array
int max = arr[0]; // Assume first element is the largest element in the array
for (int counter = 1; counter < arr.length; counter++) // Iterate through the array
{
if (arr[counter] > max) // if element is larger than my previous found max
{
max = arr[counter]; // then save the element as max
}
}
return max; // return the maximum value at the end of the array
}
public static void main(String[] args) {
int numberofslots =10;
int[] myIntArray = new int[numberofslots]; // creates an array with 10 slots of type int
Random r = new Random(7);
for (int i = 0; i < myIntArray.length; i++) // Iterate through the array 10 times
{
int x = r.nextInt();
myIntArray[i] = x; // Generate random number and add it as the i th element of the array.
}
int result = findMax(myIntArray); // calling the function for finding the largest value
System.out.println(result); // display the largest value
}
}
希望你能通過(guò)閱讀評(píng)論來(lái)理解代碼..
添加回答
舉報(bào)