java - How do I find the users number using binary search -


prompt: player chooses range (both min , max), , thinks of number in range (no need type number program). game should use binary search systematically guess player’s number. player should tell computer “too high” or “too low” or “correct” between rounds. program should continue until computer gets answer, or detects cheating (or knows answer sure). before quitting, computer should how many “rounds” (how many guesses took).

problem: after computer wrong first time , user declares high or low, can't ressign values upper , lower range

import java.util.scanner;  public class testpractice {      public static void main(string[] args) {         system.out.println("think of number");         scanner scan = new scanner(system.in);         string x = null;         string y = null;         string = null;         //get input player         system.out.println("please maximum value");          if (scan.hasnext()) {             x = scan.next();         }          system.out.println("please input min value");         if (scan.hasnext()) {             y = scan.next();         }          //parse input usuable in array         int max = integer.parseint(x);         int min = integer.parseint(y);          boolean numberguessed = true;          int numberofrounds = 0;          while(numberguessed) {             int midpoint = (max+min)/2;              numberofrounds++;              system.out.println("is number " + midpoint + " please low or high or correct");              if (scan.hasnext()) {                  = scan.next();              }              if (i.equalsignorecase("too high")) {                  min = midpoint;              }              if (i.equalsignorecase("too low")) {                  max = midpoint;                  min = 0;              }              if (i.equalsignorecase("correct")) {                  system.out.println("the number of rounds in game is" + numberofrounds);                  break;              }          }      } } 

you need use scan.nextline() instead of scan.next() , read in line including space characters why max , min never set in first place.

a scanner breaks input tokens using delimiter pattern, default matches whitespace.

more info on scanner

to loop again whole game, @ do {} while(true); iteration.

system.out.println("think of number"); scanner scan = new scanner(system.in); string playagain = "y"; string x = null; string y = null; string = null;  {     // input player     system.out.println("please maximum value");      if (scan.hasnext()) {         x = scan.next();     }      system.out.println("please input min value");     if (scan.hasnext()) {         y = scan.next();     }      // parse input usuable in array     int max = integer.parseint(x);     int min = integer.parseint(y);     int midpoint = 0;     boolean numberguessed = true;     int numberofrounds = 0;      while (numberguessed) {                  midpoint = (max + min) / 2;         numberofrounds++;         system.out.println("is number " + midpoint                 + " please press (l) low or (h) high or (c) correct");         if (scan.hasnext()) {             = scan.nextline();         }         system.out.println(i);         if (i.equalsignorecase("h")) {             min = midpoint;         } else if (i.equalsignorecase("l")) {             max = midpoint;             min = 0;         } else if (i.equalsignorecase("c")) {             system.out.println("the number of rounds in game is"                     + numberofrounds);             break;         }      }     system.out.println("press y play again");     if (scan.hasnext()) {         playagain = scan.next();     }     system.out.println("game over"); } while (playagain.equalsignorecase("y")); 

more info on while

a suggestion use simple yes/no answers h,l , c instead of asking users write word. let know.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -