有人可以向我解釋這段代碼嗎?所以我有一個充滿整數的陣列。現在代碼應該確定哪個 int 數是最大的。但我不明白在這種情況下的 if 命令。
max = 0; for(int i = 0; i < testArray.length; i ) {
if(max < testArray[i]) {
max = testArray[i];
}
}
uj5u.com熱心網友回復:
在回圈之前,我們宣告了一個名為 的整數max。這個變數將代表我們發現的最高值。然后我們開始一個 for 回圈,遍歷陣列的每個元素。對于每個值,我們將其與我們的max變數進行比較。如果max低于我們正在檢查的值,則該值是迄今為止我們發現的最高值,因此我們將該值分配給max變數。在我們遍歷整個陣列之后,max變數將包含我們找到的最高值。
int max = 0; // A variable that represents the highest value we have found
for(int i = 0; i < testArray.length; i ) {
if(max < testArray[i]) { // If max is lower than the current value...
max = testArray[i]; // ...assign the current value to max
}
}
uj5u.com熱心網友回復:
首先讓我們創建陣列[10, 100, 25, 57, 63, 96]并開始我們的回圈 max = 0,其中第一個 int 將為 10 if 將測驗為 0 < 10 true 它將去并使 max = 10 第二次將檢查 10 < 100 是否為 true它將執行該陳述句,最大值將為 100 然后第三個將檢查 100 < 25 是否為假并保持到回圈結束
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362164.html
