第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何返回包含 10 個(gè)隨機(jī)整數(shù)的數(shù)組中的最大整數(shù)?

如何返回包含 10 個(gè)隨機(jī)整數(shù)的數(shù)組中的最大整數(shù)?

泛舟湖上清波郎朗 2021-12-01 15:36:18
所以這是我在學(xué)校遇到的一個(gè)編碼問題,我不想說(shuō)“嘿伙計(jì)們?yōu)槲易鲎鳂I(yè)!”,我實(shí)際上想了解這里發(fā)生了什么。我們剛剛開始使用數(shù)組,它們讓我感到困惑,所以我正在尋找一些幫助。這是完整的問題:編寫一個(gè)程序,其中 main 方法創(chuàng)建一個(gè)具有 10 個(gè) int 類型槽的數(shù)組。為每個(gè)插槽分配一個(gè)隨機(jī)生成的整數(shù)。調(diào)用一個(gè)函數(shù),將數(shù)組傳遞給它。被調(diào)用的函數(shù)應(yīng)該將數(shù)組中最大的整數(shù)返回給您的主方法。您的主要方法應(yīng)該顯示返回的數(shù)字。使用 Random 對(duì)象生成整數(shù)。創(chuàng)建它Random r = new Random(7);生成一個(gè)隨機(jī)整數(shù)x = r.nextInt();所以,這是我到目前為止所擁有的:import java.util.Random;public class Q1 {    public static void main(String[] args) {     Random r = new Random(7);    int[] count = new int[11];    int x = r.nextInt();    for (int i = 0; i < count.length; i++)    {        count[i] = x;    }}我用 10int秒創(chuàng)建了該數(shù)組,然后使用for循環(huán)來(lái)分配隨機(jī)生成整數(shù)的每個(gè)插槽。不過(guò),我很難確定接下來(lái)要做什么。我不確定要?jiǎng)?chuàng)建什么樣的方法/函數(shù),然后如何從那里獲得最大的int并返回它。任何幫助都非常感謝,因?yàn)槲艺娴暮芟肓私膺@里發(fā)生了什么。謝謝!
查看完整描述

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 的方法。


查看完整回答
反對(duì) 回復(fù) 2021-12-01
?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

將最大值初始化為數(shù)組的第一個(gè)值。然后使用 for 循環(huán)迭代數(shù)組并使用最大值檢查數(shù)組當(dāng)前值。或者你可以sort數(shù)組并返回。祝你好運(yùn)!


查看完整回答
反對(duì) 回復(fù) 2021-12-01
?
BIG陽(yáng)

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)理解代碼..


查看完整回答
反對(duì) 回復(fù) 2021-12-01
  • 3 回答
  • 0 關(guān)注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)