The task is to write a java program to test if an array contains a specific value in java

huangapple go评论65阅读模式
英文:

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&#39;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&#39;m only a beginner in Java...
# 
# **here&#39;s my code: **

public static void main(String[] args) {
        
     Scanner input = new Scanner(System.in);
     int i,x;
     System.out.print(&quot;Input the size of the array: &quot;);
     int size = input.nextInt(); 
     int num [] = new int [size];  /* declaration */
     
      System.out.println(&quot;Enter &quot;+ size + &quot; values: \n&quot;);
       for(i=0; i &lt; num.length; i++){ 
          x = input.nextInt();
         num[i]=x;
       if ( num [i] == x );
       }
       System.out.print(&quot;Enter the search value: &quot;);
       int search = input.nextInt();
         
     if ( num[i] == search){
         System.out.println (search + &quot; is found in the array!&quot;);
     }
     else{ 
         System.out.println (&quot;Your number is not in the array.&quot;);
        
     }
    }
   }


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 + &quot; is found in the array!&quot; : &quot;Your number is not in the array.&quot;);

Alternatively, with streams:

System.out.println(Arrays.stream(num).anyMatch(x -&gt; x == search) ?
 search + &quot; is found in the array!&quot; : &quot;Your number is not in the array.&quot;);

答案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(&quot;Input the size of the array: &quot;);
    int size = input.nextInt(); 
    int num [] = new int [size];  /* declaration */
    
    //  System.out.println(&quot;Enter &quot;+ size + &quot; values: \n&quot;);
    for(i=0; i &lt; num.length; i++){ 
        System.out.println(&quot;Enter Number to add at index &quot;+i+&quot; : &quot;);
         x = input.nextInt();
        num[i]=x;
    }
    System.out.print(&quot;Enter the search value: &quot;);
    int search = input.nextInt();
    boolean condition=false;
    for(i=0; i &lt; num.length; i++){
        if ( num[i] == search){
            condition=true;
            break;
        }
    }
    if(condition==true){
        System.out.println(&quot;Number is Array!&quot;);
    }
    else{
        System.out.println(&quot;Number is not in array&quot;);
    }
}

your code is to be like this
there is logical error's in your code

  1. 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
  2. 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("您输入的数字不在数组中。");
}

Screenshot

英文:
Scanner input = new Scanner (System.in);    
int i,x;
	 System.out.print(&quot;Input the size of the array: &quot;);
	 int size = input.nextInt(); 
	 int num [] = new int [size];  /* declaration */
	 
	  System.out.println(&quot;Enter &quot;+ size + &quot; values: \n&quot;);
		for (i = 0; i &lt; num.length; i++) 
		{
			x = input.nextInt();
			num[i] = x;
			if (num[i] == x);
		}
		System.out.print(&quot;Enter the search value: &quot;);
		int search = input.nextInt();
		
		boolean flag=false;
		for( i=0;i&lt;num.length;i++) 
		{
			if (num[i] == search) {
		        flag = true;
		        System.out.println(num[i]+&quot; is found in your input array&quot;);
		        break;
		    
		    }
		}
		if (flag==false) 
		{
      System.out.println(&quot;Your number is not in the array.&quot;);
		}
}

[Screenshot][1]

huangapple
  • 本文由 发表于 2023年2月19日 13:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定