var randomLoc = Math.floor(Math.random()*5);//Math.floor向下取整,Math.random取0-1之间任意数
var location1 = randomLoc;
var location2 = randomLoc + 1;
var location3 = randomLoc + 2;
var guess;
var hits = 0;
var guesses = 0;
var isSunk = false;
while (isSunk == false) {
guess = prompt("Ready, aim, fire! (enter a number from 0-6):"); //prompt获取用户输入
if (guess < 0 || guess > 6) { //排除法,先排除错误的范围
alert("Please enter a valid cell number!");
}else {
guesses = guesses + 1;
if (guess == location1 || guess == location2 || guess == location3){ // ||或,&&且,!非
alert("HIT!");
hits = hits + 1;
if (hits == 3){
isSunk = true;
alert("You sank my battleship!");
}
}else {
alert("MISS");
}
}
}
var stats = "You took " + guesses + " guesses to sink the battleship," + " which means your shooting accuracy was " + (3/guesses);
alert(stats);
Chapter 2 Writing Real Code
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- title: Writing_Bug_Free_C_Code_Chapter_2_Know_Your_Enviro...
- 2017.10.30 Part 1 Words 1.arduous 原文:Coming home from an ...
- title: Writing Bug Free C code chapter 1 Understand Why B...
- 一、修改稿 1、开头 Whether technology makes life simpler or more ...
- 一、范文比对 刚才吃完午饭,我抽空学习了一下唐伟胜老师最新在他微信公众号《唐老雅英语写作工作室》发表的范文受益匪浅...