简体   繁体   中英

Why the last loop is not working as I expected in Java?

I am trying to solve this question in Java. However, the last loop is not working as I had expected. Now, I want to know why this is not working.

Take an array as input from the user. Search for a given number x and print the index at which it occurs.

package core_java.arrays;

import java.util.Scanner;

import static java.lang.System.in;
import static java.lang.System.out;

public class Java_Array_Exercise {
    public static void main(String[] args) {
        Scanner sc = new Scanner(in);

    // taking the array size
    out.print("Enter the size of an array: ");
    int size = sc.nextInt();
    // Designing the array
    int[] arr = new int[size];
    // Design of the array input
    int pointer = 1;
    for (int i = 0 ; i < size ; i++) {
        if (pointer == 1) {
            out.printf("Enter %dst number: ", pointer);
            arr[i] = sc.nextInt();
        } else if (pointer == 2) {
            out.printf("Enter %dnd number: ", pointer);
            arr[i] = sc.nextInt();
        } else if (pointer == 3) {
            out.printf("Enter %drd number: ", pointer);
            arr[i] = sc.nextInt();
        } else {
            out.printf("Enter %dth number: ", pointer);
            arr[i] = sc.nextInt();
        }
        pointer++;
    }   // end of array input loop

    // target number
    out.print("Enter a target from the input: ");
    int target = sc.nextInt();
    // matching the target design
    for (int i = 0 ; i < arr.length ; i++) {
        if (arr[i] == target) {
            out.printf("Target = %d has found at the index of %d", target, i);
            break;
        } else if (arr[arr.length - 1] != target ){
            out.printf("Your target = %d has not been found.", target);
            break;
        }
    }   // end of matching the target loop

  }   // main method
}   // class

The last loop is not working as I expected. 1st and last targets are working fine. However, middle targets are not working.

Sample output:

Enter the size of an array: 3
Enter 1st number: 1
Enter 2nd number: 2
Enter 3rd number: 3
Enter a target from the input: 2
Your target = 2 has not been found.

The following condition means that the last value isn't the value you loop for, that isn't a reason to stop looking for you value. As it isn't the first, and not the last, your stop, but haven't search in the middle

else if (arr[arr.length - 1] != target) {
    out.printf("Your target = %d has not been found.", target);
    break;
}

You need to have search through the whole array before being able to say "not found"

boolean found = false;
for (int i = 0; i < arr.length; i++) {
    if (arr[i] == target) {
        out.printf("Target = %d has found at the index of %d\n", target, i);
        found = true;
        break;
    }
}

if (!found) {
    out.printf("Your target = %d has not been found.\n", target);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM