If语句只返回满足条件的第一个值的索引?JAVA
我的任务是编写一个具有非空数组“温度”的类,该数组存储一年365天的温度 我的任务是写一个方法返回一年中温度最低的一天。 例如,如果数组温度={0,0,-10,-10,0,…,0}。相应的结果应为3,尽管有两个相同的值,温度最低,因为第三天第二个指数是第一个具有最低值的指数 我已经正确地写出了代码,但是,我不确定为什么在我的第二个If语句中,它只返回具有最低值的日期,而不是返回具有最低值的所有日期 这是我的密码If语句只返回满足条件的第一个值的索引?JAVA,java,for-loop,if-statement,Java,For Loop,If Statement,我的任务是编写一个具有非空数组“温度”的类,该数组存储一年365天的温度 我的任务是写一个方法返回一年中温度最低的一天。 例如,如果数组温度={0,0,-10,-10,0,…,0}。相应的结果应为3,尽管有两个相同的值,温度最低,因为第三天第二个指数是第一个具有最低值的指数 我已经正确地写出了代码,但是,我不确定为什么在我的第二个If语句中,它只返回具有最低值的日期,而不是返回具有最低值的所有日期 这是我的密码 public static int coldest(double[]
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值。我想您可能会对'return'语句的实际功能感到困惑 它的主要操作是“退出此函数”,或者更具体地说,“退出此函数的当前调用”。从这一点来看,应该很清楚,你只能这样做一次。一旦你退出,你就退出了 它的第二个操作是提供函数调用的值,即“返回”给调用方的值。函数被声明为返回某个“int”值int coldest。。。return语句提供了一个int值。原因是,如果语句中断了for循环,则在第二秒内返回。我建议你采用这种解决办法。您可以返回整数值列表,而不是返回一个整数值: 原因是在第二个if语句中返回的值打破了for循环。我建议你采用这种解决办法。您可以返回整数值列表,而不是返回一个整数值: 我的任务是编写一个方法,用 最低温度 如果是这种情况,则说明您的逻辑有缺陷,例如,以下情况没有任何意义:
if (Math.abs(minimum - temperatures[i]) < 0.000000001)
如果您的要求是获取具有最低温度的所有天数的列表,则需要返回一个数组而不是单个值,例如
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;
}
}
我不确定您是否已达到使用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;
}
}
我的任务是编写一个方法,用
最低温度
如果是这种情况,则说明您的逻辑有缺陷,例如,以下情况没有任何意义:
if (Math.abs(minimum - temperatures[i]) < 0.000000001)
如果您的要求是获取具有最低温度的所有天数的列表,则需要返回一个数组而不是单个值,例如
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;
}
}
我不确定您是否已达到使用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;
}
}
因为一旦到达return语句,函数就完成了。因为一旦到达return语句,函数就完成了。
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]