Simple guess Numbers (Java edition)

Source: Internet
Author: User

Follow the examples in the book.

However, the bug mentioned in the book, I did not look at the next chapter when the resolution of Kazakhstan.

The method of removing an array element from the Web lookup.

In fact, changing the data structure to ArrayList may be easier. :)

Gamehelper.java

ImportJava.io.*; Public classGamehelper { Publicstring Getuserinput (String prompt) {string Inputline=NULL; System.out.println (Prompt+ " "); Try{BufferedReader is=NewBufferedReader (NewInputStreamReader (system.in)); Inputline=Is.readline (); if(inputline.length () = = 0 )                return NULL; } Catch(IOException e) {System.out.println ("IOException:" +e); }        returnInputline; }}

Simpledotcom.java

 Public classsimpledotcom {int[] locationcells; intNumofhits = 0;  Public voidSetlocationcells (int[] locs) {Locationcells=locs; }         Public int[] Dellocationcells (inteleint) {         for(inti = 0; i < locationcells.length; i++) {            if(Eleint = =Locationcells[i]) {                 for(intj = i + 1; J < Locationcells.length; J + +) {Locationcells[i]=Locationcells[j]; }            }        }        returnLocationcells; }         Publicstring Checkyourself (String stringguess) {intGuess =Integer.parseint (stringguess); String result= "Miss";  for(intcell:locationcells) {            if(Guess = =cell) {Result= "Hit"; Numofhits++;                Dellocationcells (guess);  Break; }        }                if(Numofhits = =locationcells.length) {result= "Kill";        } System.out.println (Result); returnresult; }}

Simpledotcomtestdrive.java

 Public classsimpledotcomtestdrive { Public Static voidMain (string[] args) {intnumofguesses = 0; Gamehelper Helper=NewGamehelper (); Simpledotcom thedotcom=Newsimpledotcom (); intRandomnum = (int) (Math.random () * 5); int[] locations = {Randomnum, Randomnum + 1, Randomnum + 2};        Thedotcom.setlocationcells (locations); BooleanIsAlive =true;  while(IsAlive = =true) {String guess= Helper. Getuserinput ("Enter A number"); String result=thedotcom.checkyourself (guess); Numofguesses++; if(Result.equals ("Kill") ) {isAlive=false; System.out.println ("You took" + numofguesses + "guesses."); }                    }            }}

Output:

Simple guess Numbers (Java edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.