英文:
Checking if an array contains two numbers
问题
import java.util.Scanner;
public class checker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] numbers = new int[length];
boolean broken = false;
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int j = 1; j < length; j++) {
if (numbers[j] == n && numbers[j-1] == m || numbers[j] == m && numbers[j+1] == n || numbers[j] == m && numbers[j-1] == n || numbers[j] == n && numbers[j+1] == m) {
broken = false;
} else {
broken = true;
}
}
if (broken) {
System.out.println("false");
} else {
System.out.println("true");
}
}
}
英文:
i have just started with Java and facing a problem right now, which i cannot solve after 2hrs of checking code.
When i input :
7 ( as length)
6 3 4 8 3 2 6 (as array)
8 3 (as numbers to check)
it Writes false, but clearly they are included in the exact order.
Please help me trying to understand what the problem is
here is the Code:
import java.util.Scanner;
public class checker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
int[] numbers = new int[length];
boolean broken = false;
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for (int j = 1; j < length; j++) {
if (numbers[j] == n && numbers[j-1] == m || numbers[j] == m && numbers[j+1] == n || numbers[j] == m && numbers[j-1] == n || numbers[j] == n && numbers[j+1] == m) {
broken = false;
} else {
broken = true;
}
}
if (broken) {
System.out.println("false");
} else {
System.out.println("true");
}
}
}
</details>
# 答案1
**得分**: 0
你在评论中提到,你想要检查 {n,m} 和 {m,n},那么为什么要增加更多的条件呢?下面的代码足够了:
```java
for (int j = 0; j < length-1; j++) {
if (numbers[j] == n && numbers[j+1] == m || numbers[j] == m && numbers[j+1] == n) {
broken = false;
break;
} else {
broken = true;
}
}
循环从 0(第一个数组元素)到最后 -1,因为 if
语句与 j+1 进行比较。一旦找到匹配项,必须跳出循环,否则可能会再次将 broken
设置为 false。
英文:
As you mentioned in a comment, you want to check on {n,m} and {m,n} so why do you have more conditions?
The following is enough:
for (int j = 0; j < length-1; j++) {
if (numbers[j] == n && numbers[j+1] == m || numbers[j] == m && numbers[j+1] == n) {
broken = false;
break;
} else {
broken = true;
}
}
The loop goes from 0 (=first array element) to the last -1 because the if compares with j+1. Once you have found a match you have to break out of the loop otherwise you risk to set broken
to false again.
答案2
得分: 0
你也可以在不使用布尔值的情况下实现这一点,查看下面调整后的代码。
import java.util.Scanner;
public class checker {
public static void main(String[] args) {
int howManyValuesPresent = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("数组长度");
int length = scanner.nextInt();
int[] numbers = new int[length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for(int number: numbers){
if(number == n || number == m){
howManyValuesPresent++;
}
}
if (howManyValuesPresent > 0) {
System.out.println("true,你选择的数字出现:" + howManyValuesPresent + " 次在数组中");
} else {
System.out.println("false,你选择的数字出现:" + howManyValuesPresent + " 次在数组中");
}
}
}
英文:
You could also achieve this without using a boolean, check tweaked code below.
import java.util.Scanner;
public class checker {
public static void main(String[] args) {
int howManyValuesPresent = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Length of array");
int length = scanner.nextInt();
int[] numbers = new int[length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scanner.nextInt();
}
int n = scanner.nextInt();
int m = scanner.nextInt();
for(int number: numbers){
if(number == n || number == m){
howManyValuesPresent++;
}
}
if (howManyValuesPresent>0) {
System.out.println("true, Your selected numbers appear: " + howManyValuesPresent + " times in array");
} else {
System.out.println("false, Your selected numbers appear: " + howManyValuesPresent + " times in array");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论