Skip to content
Advertisement

Java picks any random number as the correct answer

I want to develop a “periodic table tester” which asks you the atomic number of any random element. If you answer wrong, it will tell you the correct answer.
Here’s my code:

JavaScript

Its output should be like

JavaScript

But, it’s like:

JavaScript

How should I solve the problem?

Advertisement

Answer

You’re using Arrays.binarySearch() on the wrong type of data set. It needs to take a sorted input to work properly.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement