jeudi 28 septembre 2017

Please help me with the code - trying to figure out for the whole night

Could anybody help with a code. 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 interprete 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");

            }               

            }

        }

 }




Aucun commentaire:

Enregistrer un commentaire