英文:
The task is to write a java program to test if an array contains a specific value in java
问题
我感到困惑,不知道应该添加或更改循环的内容。
可以有人帮助我,并告诉我应该做什么来运行我的代码吗?这对我将会有很大帮助。我只是一个Java的初学者...
这是我的代码:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i, x;
System.out.print("输入数组的大小:");
int size = input.nextInt();
int num[] = new int[size]; /* 声明 */
System.out.println("输入 " + size + " 个值:\n");
for (i = 0; i < num.length; i++) {
x = input.nextInt();
num[i] = x;
if (num[i] == x);
}
System.out.print("输入要搜索的值:");
int search = input.nextInt();
if (num[i] == search) {
System.out.println(search + " 在数组中找到了!");
} else {
System.out.println("您输入的数字不在数组中。");
}
}
我的任务的示例输出(使用Scanner):
输入数组的大小:4
输入值:
14
15
16
17
输入要搜索的值:14
14 在您的输入数组中找到了。
<details>
<summary>英文:</summary>
I got confused and didn't know what to add or change the loops.
Can someone please help me and tell me what I should do in order to run my code? It would be a big help to me. I'm only a beginner in Java...
#
# **here's my code: **
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i,x;
System.out.print("Input the size of the array: ");
int size = input.nextInt();
int num [] = new int [size]; /* declaration */
System.out.println("Enter "+ size + " values: \n");
for(i=0; i < num.length; i++){
x = input.nextInt();
num[i]=x;
if ( num [i] == x );
}
System.out.print("Enter the search value: ");
int search = input.nextInt();
if ( num[i] == search){
System.out.println (search + " is found in the array!");
}
else{
System.out.println ("Your number is not in the array.");
}
}
}
The sample output of my task is (using scanner):
Input the size of the array: 4
Enter the values:
14
15
16
17
Enter the search value: 14
14 is found in your input array.
</details>
# 答案1
**得分**: 0
你需要循环来比较数组的每个元素与搜索值。
```java
boolean found = false;
for (int x : num)
if (x == search) {
found = true;
break;
}
System.out.println(found ? search + " 在数组中找到了!" : "数组中没有您的数字。");
或者,使用流(Streams)实现:
System.out.println(Arrays.stream(num).anyMatch(x -> x == search) ?
search + " 在数组中找到了!" : "数组中没有您的数字。");
英文:
You need a loop to compare each element of the array with the search value.
boolean found = false;
for (int x : num)
if (x == search) {
found = true;
break;
}
System.out.println(found ? search + " is found in the array!" : "Your number is not in the array.");
Alternatively, with streams:
System.out.println(Arrays.stream(num).anyMatch(x -> x == search) ?
search + " is found in the array!" : "Your number is not in the array.");
答案2
得分: 0
你的代码应该像这样
你的代码中有逻辑错误:
1. 要查找一个元素,你必须遍历整个数组,这可以通过循环实现,所以你代码中最后的 if else 语句应该放在一个循环中。
2. 第一个循环中由于后置递增,在循环结束后,i 的值比数组的最后一个索引大 1,因此在 `if ( num[i] == search)` 的条件语句中,索引不在数组范围内,会导致错误。
例如,我输入数组大小为 3,在 num[0] 输入 1,在 num[1] 输入 2,在 num[2] 输入 3,当循环结束时,i 的值变成了 3,所以在条件语句中(if else)就会变成 `if(num[3] == search)`,但是数组中不存在第 3 个索引,因此会产生错误。
英文:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i,x;
System.out.print("Input the size of the array: ");
int size = input.nextInt();
int num [] = new int [size]; /* declaration */
// System.out.println("Enter "+ size + " values: \n");
for(i=0; i < num.length; i++){
System.out.println("Enter Number to add at index "+i+" : ");
x = input.nextInt();
num[i]=x;
}
System.out.print("Enter the search value: ");
int search = input.nextInt();
boolean condition=false;
for(i=0; i < num.length; i++){
if ( num[i] == search){
condition=true;
break;
}
}
if(condition==true){
System.out.println("Number is Array!");
}
else{
System.out.println("Number is not in array");
}
}
your code is to be like this
there is logical error's in your code
- to find an element you must traverse through array which can be achieved using loop so the last if else statement in your code must be in a loop
- as due to the post increment in first loop after the completion of loop the value of i is one greater than last index of array so in if else statement at
if ( num[i] == search)
index is not found in array due to which it generates error.
for example I enter array size 3 and at num[0] I enter 1 at num1 I enter 2 and at num[2] I enter 3 after the loop finishes the value of i become 3 when it come to conditional statement (if else) so it become if(num[3] == search )
but 3 index is not present in array so it generates error.
答案3
得分: -1
Scanner input = new Scanner(System.in);
int i, x;
System.out.print("输入数组的大小: ");
int size = input.nextInt();
int num[] = new int[size]; /* 声明 */
System.out.println("输入 " + size + " 个值:\n");
for (i = 0; i < num.length; i++) {
x = input.nextInt();
num[i] = x;
if (num[i] == x);
}
System.out.print("输入要搜索的值: ");
int search = input.nextInt();
boolean flag = false;
for (i = 0; i < num.length; i++) {
if (num[i] == search) {
flag = true;
System.out.println(num[i] + " 在您输入的数组中找到了");
break;
}
}
if (flag == false) {
System.out.println("您输入的数字不在数组中。");
}
英文:
Scanner input = new Scanner (System.in);
int i,x;
System.out.print("Input the size of the array: ");
int size = input.nextInt();
int num [] = new int [size]; /* declaration */
System.out.println("Enter "+ size + " values: \n");
for (i = 0; i < num.length; i++)
{
x = input.nextInt();
num[i] = x;
if (num[i] == x);
}
System.out.print("Enter the search value: ");
int search = input.nextInt();
boolean flag=false;
for( i=0;i<num.length;i++)
{
if (num[i] == search) {
flag = true;
System.out.println(num[i]+" is found in your input array");
break;
}
}
if (flag==false)
{
System.out.println("Your number is not in the array.");
}
}
[Screenshot][1]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论