[英]How do I iterate over elements of an array in Java while I am inside of a for loop to iterate over the elements of an array?
我正在学习 java,正在学习 Java 中的 arrays。我想尝试编写一个程序来遍历数组中的每个元素,并使用程序查看元素是增加、减少还是数组的一部分正在增加的方法并且数组的一部分正在减少。 我对如何执行此操作感到很困惑,并希望获得有关我需要修复的内容的一些指导。 我猜我的尝试可能与我想要发生的事情相去甚远。
我不太确定如何遍历数组并测试每个单独的元素。 我试过了,但我做错了。 我试图制作一个 for 循环,它将遍历数组的元素。 在 for 循环中,我尝试制作一个 if 语句,我希望它通过数组中的每个元素到 go,然后检查数组的顺序是否为递增顺序,然后是 output“递增”、递减顺序和 output“递减” ”,或者如果数组的一部分在增加而一部分在减少,我希望 output 是“增加和减少”。 (例如:{1,3,5,7,4,2})1,3,5,7 增加,4,2 减少。 所以我有一个循环来遍历数组中的每个元素,然后在第一个循环中进行另一个循环,检查数组中的元素是增加还是减少,并输出增加或减少。
public class increasingOrDecreasing {
public static void main(String[] args) {
int[] array1 = { 3, 8, 13, 29, 52, 81 };
int[] array2 = { 77, 66, 55, 33, 22, 11 };
int[] array3 = { 20, 30, 60, 40, 15, 2 };
int[] array4 = { 10, 30, 50, 50, 60, 80 };
// call the method
increaseOrDecrease(array1);
increaseOrDecrease(array2);
increaseOrDecrease(array3);
increaseOrDecrease(array4);
}
// create the method
public static int increaseOrDecrease(int[] myArray) {
// make a loop to check if all the integers of the array are in either
// increasing or decreasing order or both
for (int i = 0; i < myArray.length; i++) {
// if the array is increasing
// What I wanted is for it to start at array i = 0 and have [i] go up by 1 until it loops through the end // of the array. Then if each element is less than the subsequent element it would output "Increasing": I // did not do this correctly and would like some help. I guess all I did is check if myArray[i] is less
// than myArray[i+1]
// if the array was {1, 3, 6, 8} then the method would go through each element and find that index [0] <
// index [1] and index[1] < index [2] and index[2] < index [3] and output Increasing and return a value of // 1 because the method would find that all the numbers are ordered smallest to largest.
if (myArray[i] < myArray[i++]) {
System.out.println("Increasing");
return 1;
}
// this is incorrect and does not do what I was hoping it would.
// if the array is decreasing
else if (myArray[i] > myArray[i++]) {
System.out.println("Decreasing");
return 2;
}
// this is also incorrect
// if the array is increasing and decreasing
else (myArray[i] < myArray[i++] && myArray[i] > myArray[i++]) {
System.out.println("Both increasing and decreasing");
return 3;
}
}
return 0;
}
}
这就是我想要的:检查每个元素并查看该元素是否小于数组中的下一个元素。 对于第一个数组 {3,8,17,25,89,94} 它将遍历数组。 索引 0 是 3,小于索引 1。它转到元素 1,即 8,索引 1 小于索引 2,即 17。这种情况会一直发生,直到该方法循环遍历数组中的所有元素,它会 output增加。 如果数组中的每个元素都小于后续元素,则检查顺序是否从最小到最大,然后该方法将 output 递增。 然后,如果数组仅反向减少,我会做同样的事情。 我没有正确执行此操作,请提供帮助。
这是我尝试检查数组是否从最小到最大的方法。 我认为它只是检查了 if [i] < [i++] 但没有通过整个数组检查 go 。
for (int i = 0; i < myArray.length; i++) {
// if the array is increasing
// What I wanted is for it to start at array i = 0 and have [i] go up by 1 until it loops through the end // of the array. Then if each element is less than the subsequent element it would output "Increasing": I // did not do this correctly and would like some help. I guess all I did is check if myArray[i] is less
// than myArray[i+1]
// if the array was {1, 3, 6, 8} then the method would go through each element and find that index [0] <
// index [1] and index[1] < index [2] and index[2] < index [3] and output Increasing and return a value of // 1 because the method would find that all the numbers are ordered smallest to largest.
if (myArray[i] < myArray[i++]) {
System.out.println("Increasing");
return 1;
}
public class Eli {
public static void main(String[] args) {
int myArray[] = {12,5,4,7,2};
boolean increasing = false, decreasing = false;
for (int i = 0; i < myArray.length-1; i++) {
if (myArray[i] < myArray[i+1]) {
increasing = true;
}
if (myArray[i] > myArray[i+1]) {
decreasing = true;
}
}
if (increasing == true && decreasing == true) {
System.out.println("Increasing and decreasing");
}
else {
if (increasing == true)
System.out.println("Increasing");
else System.out.println("Decreasing");
}
}
}
注意:如果您确定下跌后上涨不被视为“上涨和下跌”,则循环中的第一个条件将是:
if (decreasing == true)
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.