N
N
Nodir Malikov2021-02-22 11:23:03
Java
Nodir Malikov, 2021-02-22 11:23:03

Finding a number in an array. How to display only one answer?

Finding a number in an array:

public class Main {
    interface GetNum {
        void num(int[] x);
    }

    public static void main(String[] args) {
        int[] arr = {15, 16, 18, 19, 22, 0, -2, -4, 44};

        GetNum getNum = (x) -> {
            for (int j : arr) {
                if (j != -4) {
                    continue;
                } else {
                    System.out.println("Есть!");
                    break;
                }
            }
            System.out.println("Нету!");
        };

        getNum.num(arr);
    }
}


But the message "No!" output even though there is a number in the array. How can I make it so that if the number is present, this message is not displayed?

https://repl.it/join/plhlmrrx-lotvw07

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan., 2021-02-22
@Fayo

When exiting the loop, it will always write what is not found, you still need to use the flag.
For example so.
boolean f = false;
for (int j : arr) {
if (j == x) {
f = true;
break;
}
}
if (!f)
{System.out.println("No!");}
else
{System.out.println("Yes!");}

N
Nikita, 2017-10-26
@nano_e_t_4

This is invalid json. Accordingly, this is not json at all. Or turn it into json or write a parser for this miracle.

I
index0h, 2017-10-27
@index0h

JSON is parsed using the standard json package. If in the structure - the fields must be exportable, in those with a small letter - inserting data will not be inserted. Well, unless you implement the json.Unmarshaler interface

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question