Java 方法应用实例01

来源:互联网 发布:淘宝售前客服可不可信 编辑:程序博客网 时间:2024/04/29 08:57

Sink a Dot Com

花了将近三个小时从看懂和敲出来,还是不给力啊

package dotcom;// HeadFirstJava "Sink a Dot Com"public class SimpleDotComTest {public static void main(String[] args) {    int numOfGuesses = 0;    // getting user input for now    GameHelper helper = new GameHelper();    // instantiate a SipleDotCom object    SimpleDotCom  dot = new SimpleDotCom();    // make the random number for the first cell, and use it to make the cell locations array    int randomNum = (int)(Math.random()*5);    int [] locations = {randomNum,randomNum+1,randomNum+2};    // invoke the setter method on the dot com    dot.setLocationCells(locations);    boolean isAlive =true;    while(isAlive == true) {        String guess = helper.getUserInput("enter a number");        String result = dot.checkYourself(guess);        numOfGuesses++;        if(result.equals("kill")) {            isAlive = false;            System.out.println("You took"+ numOfGuesses + "guess");        }    }}}
package dotcom;public class SimpleDotCom {int [] locationCells;int numOfHits = 0;    public void setLocationCells(int[] locations) {        locationCells = locations;    }    public String checkYourself(String StringGuess) {        //covert the String to int    int guess = Integer.parseInt(StringGuess);    // make a variable to hold the result we'll return. Put "miss" in as the default     String result = "miss";    // repeat with each cell in the locationCells arrays(each location of object)    for(int cell:locationCells) {        // compare the user guess to this element(cell) in the arrays        if (guess == cell) {            // we got a hit            result = "hit";            numOfHits++;            break;        }    }    // if number of hits is 3    if(numOfHits == locationCells.length) {        result = "Kill";    }    System.out.println(result);    return result;    }}
package dotcom;import java.io.*;public class GameHelper {    public String getUserInput(String prompt) {        String inputLine=null;        System.out.println(prompt + " ");        try {            BufferedReader is = new BufferedReader(new InputStreamReader(System.in));            inputLine = is.readLine();            if(inputLine.length() == 0) return null;        } catch (IOException e) {System.out.println("IOException:" + e);        }        return inputLine;    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 2个月宝宝不吃奶怎么办 孕妇尿隐血1十是怎么办 尿葡萄糖2个加怎么办 孕妇尿葡萄糖2加怎么办 尿的蛋白高怎么办治疗 孕妇尿蛋白高是怎么办 孕妇尿细菌数高怎么办 孕妇尿细菌很高怎么办 尿微量总蛋白高怎么办 衣服上碳素笔油怎么办 卡油泵总成坏了怎么办 汽车燃油泵坏了怎么办 新车汽油泵坏了怎么办 墓地的树枯死了怎么办 大车尿素不烧了怎么办 不烧尿素限扭怎么办 汽车首保过期了怎么办 假体隆鼻后透光怎么办 熊猫血有抗体了怎么办 rh阴性血怀二胎怎么办 熊猫血怀二胎了怎么办 rh阴性血要二胎怎么办 全血粘度都偏高怎么办 血脂高血粘度高怎么办 全血粘度值1偏高怎么办 全血粘度3偏高怎么办 血粘度高的症状怎么办 粉瘤感染化脓了怎么办 乌药剂量用大了怎么办 水卡消磁了怎么办妙招 电卡消磁了怎么办妙招 入园磁卡消磁了怎么办 透析中静脉压高怎么办 腰间盘突出压迫神经腿疼怎么办 肺热引起的发烧怎么办 肺热引起的痘痘怎么办 冰箱压条的霉点怎么办 白色的布鞋变黄怎么办 白鞋橡胶变黄了怎么办 肝功能检查总胆汁酸高怎么办 吃丹参滴丸尿血怎么办