-2

I need my program to pick a number in between two numbers (min and max) that I had selected, then I have 5 tries to figure out which number he has in mind. But the problem is that computer doesn't really interpret the limits right - picked number is sometimes outside of the extremes. min=3 max=6 - Why do I get as a result the correct number is 7?

The code is here:

import java.util.*;

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

    int min = sc.nextInt();

    int max = sc.nextInt();

    System.out.println("insert a number");

        int rand = min + (int)(Math.random() * ((max - min) + 1));

        for (int i = 0; i <=5; i++){

        int number = sc.nextInt();

            if (number == rand){

                System.out.println("congratulations! :D");
                break;
            }

            else if (i < number){

            System.out.println("The number is too big");

            }

            else if (i > number){

            System.out.println("the number is too small");

            }

            else{

            System.out.println("Try again");

            }               

            }

        }

 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Tadej
  • 1
  • 1
    Check your conditions in else-if part first. why are you comparing i and number ? – Adeel Sep 28 '17 at 06:53
  • 1
    typo `rand` instead of `number` – Naman Sep 28 '17 at 06:54
  • Another option instead using `Math` is using `Random`, which has an [`Random#nextInt`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt--) method. – Shirkam Sep 28 '17 at 06:57
  • 3
    Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with a more specific question. – Joe C Sep 28 '17 at 06:59

1 Answers1

1

The problem is in your if statements that check the user's answers. You need to compare the user's input number to the random number rand. Instead you are comparing the loop counter i to the input.

Another issue is that your for loop performs 6 iterations, not 5. To fix this, change i <= 5 to i < 5. This is because i starts at 0, not 1.

Lastly, to display the try again message, move it outside of the for loop. Then change the break to a return so that it isn't displayed when the answer is correct. Here are all of the fixes put together:

for (int i = 0; i < 5; i++) {
    int number = sc.nextInt();

    if (number == rand) {
        System.out.println("congratulations! :D");
        return;
    } else if (rand < number) {
        System.out.println("The number is too big");
    } else if (rand > number) {
        System.out.println("the number is too small");
    }
}

System.out.println("Try again");

Your random generation is fine - it seems to follow this post.

bgfvdu3w
  • 1,548
  • 1
  • 12
  • 17