[英]If statement returns index of only the first value meeting condition? Java
我的任務是編寫一個帶有非空數組“溫度”的類,該數組存儲一年 365 天的溫度。
我的任務是編寫一個方法,返回一年中溫度最低的那一天。 例如,如果數組temperatures = {0,0,-10,-10,0,......,0}
。 相應的結果應該是 3,盡管有兩個相等的值且該集合的最低溫度,因為第 3 天(第二個索引)是第一個具有最低值的。
我已經正確地寫出了代碼,但是,我不確定為什么在我的第二個 If 語句中,它只返回具有最低值的那一天,而不是所有具有最低值的日子。
這是我的代碼
public static int coldest(double[] temperatures) {
double minimum = temperatures[0];
for (double temp : temperatures) {
if (temp < minimum) {
minimum = temp;
}
}
for (int i = 0; i < temperatures.length; i++) {
if (Math.abs(minimum - temperatures[i]) < 0.000000001) {
return i + 1;
}
}
return -1;
}
例如,如果我定義double[] a = {-5,2,-5,2,-5,......2};
,肯定在第二個 For 循環中,它將返回 1,3,5,7... 因為所有這些天都滿足 If 標准,而不僅僅是 1。
如果我沒有把我的問題寫得很清楚,我很抱歉,這是我第一次在這里提問。
我認為您可能對“return”語句的實際作用感到困惑。
它的主要操作是“退出這個函數”,或者更具體地說,“退出這個函數的當前調用”。 從那以后,應該很清楚你只能這樣做一次。 一旦你退出,你就退出了。
它的次要作用是提供函數調用的值,即什么值要“返回”給調用者。 該函數被聲明為返回一些“int”值( int coldest(...)
),並且 return 語句提供了(單個)int 值。
if
語句中斷您的for
循環, if
原因是第二個return
。 我建議你采用這種解決方案。 您可以返回Integer
數值列表,而不是返回一個int
值:
public static List<Integer> coldest(double[] temperatures) {
double minimum = temperatures[0];
List<Integer> arrayList = new ArrayList<>();
for (double temp : temperatures) {
if (temp < minimum) {
minimum = temp;
}
}
for (int i = 0; i < temperatures.length; i++) {
if (Math.abs(minimum - temperatures[i]) < 0.000000001) {
arrayList.add((i + 1));
}
}
return arrayList;
}
我的任務是編寫一個方法,返回一年中溫度最低的那一天。
如果是這種情況,則您的邏輯有缺陷,例如以下條件沒有任何意義:
if (Math.abs(minimum - temperatures[i]) < 0.000000001)
請按以下步驟操作:
public class Main {
public static void main(String args[]) {
System.out.println(coldest(new double[] { 0, 0, -10, -10, 0, 0 }));
}
public static int coldest(double[] temperatures) {
if (temperatures == null || temperatures.length == 0) {
return -1;
}
double minimum = temperatures[0];
for (double temp : temperatures) {
if (temp < minimum) {
minimum = temp;
}
}
for (int i = 0; i < temperatures.length; i++) {
if (minimum == temperatures[i]) {
return i + 1;
}
}
return -1;
}
}
輸出:
3
如果您的要求是獲取具有最低溫度的所有天數的列表,則需要返回一個數組而不是單個值,例如
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
// Test
System.out.println(Arrays.toString(coldestDays(new double[] { 0, 0, -10, -10, 0, 0 })));
}
public static int[] coldestDays(double[] temperatures) {
if (temperatures == null || temperatures.length == 0) {
return new int[0];
}
double minimum = temperatures[0];
int count = 0;// To store the required size of the array
for (double temp : temperatures) {
if (temp < minimum) {
minimum = temp;
}
}
for (double t : temperatures) {
if (t == minimum) {
count++;
}
}
int[] minTemps = new int[count];// Create the array
int index = 0;
for (int i = 0; i < temperatures.length; i++) {
if (minimum == temperatures[i]) {
minTemps[index++] = i + 1;// Store the (index +1 ) of the minimum temperatures
}
}
return minTemps;
}
}
輸出:
[3, 4]
不知道你有沒有達到使用Java集合的水平。 如果是,您可以使用ArrayList
,在這種情況下,您不需要先找到最低溫度的天數來創建適當大小的數組。
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String args[]) {
// Test
System.out.println(coldestDays(new double[] { 0, 0, -10, -10, 0, 0 }));
}
public static List<Integer> coldestDays(double[] temperatures) {
if (temperatures == null || temperatures.length == 0) {
return new ArrayList<Integer>();
}
double minimum = temperatures[0];
List<Integer> days = new ArrayList<Integer>();
for (double temp : temperatures) {
if (temp < minimum) {
minimum = temp;
}
}
for (int i = 0; i < temperatures.length; i++) {
if (minimum == temperatures[i]) {
days.add(i + 1);// Store the (index +1 )of the minimum temperatures
}
}
return days;
}
}
輸出:
[3, 4]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.