Java number guessing game weird output -


public class test {     public static void main(string[] args) {         system.out.println("welcome siddharth's number guessing game. \nthink of number between 1 , 100 , press 1 continue");         scanner x= new scanner(system.in);         if (x.nextint()==1)         {         int high=100;            int low=0;         int guess=(high+low)/2;         system.out.println("is number "+ guess+"? press 0 if yes, press 1 if number higher number or press 2 if number lower number!");         scanner y= new scanner(system.in);         int ans=y.nextint();            while(ans!=0)            {                if (ans==1)                {                    low=ans;                     guess=(high+low)/2;                     system.out.println("is number "+ guess+"? press 0 if yes, press 1 if number higher number or press 2 if number lower number!");                     scanner y1= new scanner(system.in);                     ans=y1.nextint();                }                else if (ans==2)                {                    high=ans;                     guess=(high+low)/2;                     system.out.println("is number "+ guess+"? press 0 if yes, press 1 if number higher number or press 2 if number lower number!");                     scanner y2= new scanner(system.in);                     ans=y2.nextint();                }             }            system.out.println("the number thought is"+guess+"! playing!");         }         else         {             system.out.println("no problem. restart program , press 1 when ready!");         }     }  } 

i have thought number 22, , entered 2 when asked me if number 50. straight away went 1. whatever enter, showing 1 output instead of going 50, 25, 12 etc. have done same in python , c , work perfectly.

ok, so, first problem assign ans high , low instead of guess.

then, if can give advices:

  1. don't create new scanner everytime. instead, use same x scanner
  2. don't put common actions inside each if/else block. instead, put common operations outside.
  3. you can give instructions (1 higher, etc) once, , ask "is number xx? (0:yes, 1:higher, 2:lower)". more readable.

here improved version, sure can make better:

public static void main( string[] args ){     system.out.println( "welcome siddharth's number guessing game. \nthink of number between 1 , 100 , "             + "press 1 continue" );      scanner x = new scanner( system.in );      if( x.nextint() == 1 ){          int high = 100;         int low = 0;         int guess = ( high + low ) / 2;          system.out.println( "is number " + guess + "? press 0 if yes, press 1 if number higher " +                 "this number or press 2 if number lower number!" );           while( true ){              int ans = x.nextint();             if( ans == 0 ){                 break;              }else if( ans == 1 ){                 low = guess;              }else if( ans == 2 ){                 high = guess;              }              guess = ( high + low ) / 2;             system.out.printf( "is number %d ? (0: yes, 1: higher, 2:lower) ", guess );         }          system.out.println( "the number thought of " + guess + "! playing!" );      }else{         system.out.println( "no problem. restart program , press 1 when ready!" );     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -