你好你好嗎?=) 我是 Java 新手,目前,我正在學習陣列和回圈,目前我真的很掙扎。
這是我的作業:撰寫一個公共方法 int[] findMinMaxPrices(int[] price)。它接受一個價格陣列并回傳一個新陣列。
空,如果陣列為空。如果價格陣列中的最高和最低價格相同,則僅回傳一個元素。如果價格陣列同時包含最低價格和最高價格,則僅回傳兩個元素。最低價格應該先出現,然后是最高價。
只能使用 for 回圈。
我會在這里使用一些幫助:在這種情況下如何回傳一個空陣列?我在草稿中做了很多次,不幸的是,它在這里不起作用。
我如何在這里使用 for 回圈?
你能給我一些提示或建議嗎?
先感謝您!)
import java.util.Arrays;
public class QuadraticEquationSolver {
public int[] findMinMaxPrices(int[] prices) { // I know it's a mess, but I'm just learning =)
Arrays.sort(prices);
int empty [] = {};
int first [] = Arrays.copyOf(prices, 1);
int a = prices[0];
int b = prices[prices.length-1];
int second [] = new int[] {a,b};
if(prices[0] == prices[prices.length-1]) {
return first;
}
else if(prices[0] < prices[prices.length-1]) {
return second;
}else{
return empty;
//return new int[0]; I tried to use this here, didn't work =(
}
}
public static void main(String[] args) {
QuadraticEquationSolver shop = new QuadraticEquationSolver();
//Should be [50, 1500]
int[] prices = new int[] {100, 1500, 300, 50};
int[] minMax = shop.findMinMaxPrices(prices);
System.out.println(Arrays.toString(minMax));
//findMaxPrices(new int[] {10, 50, 3, 1550}), returns [3, 1550]
//findMaxPrices(new int[] {}), returns []
//findMaxPrices(new int[] {50, 50}), returns [50]
}
}
uj5u.com熱心網友回復:
您回傳一個空陣列的方式與呼叫時創建空陣列的方式相同findMaxPrices(new int[]{}),只需使用new int[]{}.
根據您的要求,您不需要對陣列進行排序,因為您可以將最小值和最大值存盤在區域變數中,然后在回傳的陣列中添加最大值之前的最小值。
您缺少的一件重要事情是檢查prices. 使用prices.length您可以獲得陣列中有多少個值。在嘗試使用索引訪問陣列之前,請務必檢查陣列的長度,否則可能會出現IndexOutOfBounds例外。使用它,您可以立即回傳 whenprices.length == 0因為陣列中沒有值并且您需要回傳一個空陣列。如果prices.length == 1我們在陣列中只有一個值。這個值必須是最小值和最大值,所以我們可以回傳一個包含這個值的陣列。在所有其他情況下,我們可以使用for回圈遍歷陣列中的所有元素。如果我們發現一個小于/大于當前最小值/最大值的值,我們將其設定為新的最大值。為此,我們需要首先將最小值和最大值初始化為最大/最小的可能值。Java 有常量Integer.MAX_VALUE和Integer.MIN_VALUE這種東西。最后但并非最不重要的一點是,我們需要檢查最大值和最小值是否相同。在這種情況下,我們只回傳一個元素,否則我們回傳兩個,但在最大值之前是最小值。
public class Application {
public static void main(String[] args) {
System.out.println(Arrays.toString(findMinMaxPrices(new int[]{100, 1500, 300, 50})));
System.out.println(Arrays.toString(findMinMaxPrices(new int[]{})));
System.out.println(Arrays.toString(findMinMaxPrices(new int[]{50, 50})));
}
public static int[] findMinMaxPrices(int[] prices) {
// we can simply check the array length. If it is zero we return an empty array
if(prices.length == 0) return new int[]{};
else if(prices.length == 1) return new int[prices[0]]; // if we only have one element that one element must be the minimum and maximum value
// array length is not zero or one -> we need to find minimum and maximum
int min = Integer.MAX_VALUE; // set to maximal possible int value
int max = Integer.MIN_VALUE; // set to minimal possible int value
for (int i = 0; i < prices.length; i ) {
int currentPrice = prices[i];
if(currentPrice < min) min = currentPrice;
if(currentPrice > max) max = currentPrice;
}
// now we have minimum and a maxium value
// if they are the same only return one value
if(min == max) return new int[]{max};
// otherwise return both, but minumum first
return new int[]{min, max};
}
}
預期輸出:
[50, 1500]
[]
[50]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/460079.html
