写一个竞猜游戏,用户必须猜一个秘密的数字,在每次猜完后程序会告诉用户他猜的数是太大了还是太小了,直到猜测正确,最后打印出猜测的次数。如果用户连续猜测同一个数字则只算一次。
我就是不知道怎么实现“如果用户连续猜测同一个数字则只算一次。”
没有思路,下面附上我写的代码,就是不知道实现最后一句。
如果有大神看到的话帮忙回答下,真的是想不出来。
回复内容:
涉及到javascript中的一个很有用的点:把object当hash来用/**
*
* 用object当做hash来记录猜过的数字
* 假设把所有猜过的数字存在一个array中, 其中有重复的数字
* [1, 2, 5, 6, 3, 5, 1, 6, 2, 6]
*
*/
var guesses = [1, 2, 5, 6, 3, 5, 1, 6, 2, 6];
var guessesHash = {};
var guessesCount = 0; // total guess count exclude duplicated guesses
for (var i = 0; i < guesses.length; i++) {
if (!guessesHash[guesses[i]]) {
guessesHash[guesses[i]] = true;
guessesCount++;
}
}
console.log(guessesCount); // should be 5
/**
*
* 当for loop中的i = 4时,guessesHash为:
* {
* ‘1’: true,
* '2': true,
* '3': true,
* '5': true,
* '6': true,
* }
* 当for loop中的i = 5时,因为guessesHash[guesses[i]]是true,
* 所以guessesCount++不会执行。
* 换句话说,for loop中一旦遇到重复的数字guessesCount++就不会执行
*
*
*
*/
用数组或者object把输入过的数存起来即可。<span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">history</span><span class="o">=</span><span class="p">{},</span>
<span class="nx">secret</span><span class="o">=</span><span class="mi">4</span><span class="p">;</span> <span class="c1">// https://www.xkcd.com/221/</span>
<span class="k">do</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">input</span><span class="o">=</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">prompt</span><span class="p">(</span><span class="s1">'输入数字: '</span><span class="p">));</span>
<span class="nx">history</span><span class="p">[</span><span class="nx">input</span><span class="p">]</span><span class="o">=</span><span class="kc">true</span><span class="p">;</span>
<span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o">></span><span class="nx">secret</span><span class="p">)</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个小'</span><span class="p">);</span>
<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o"><</span><span class="nx">secret</span><span class="p">)</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个大'</span><span class="p">);</span>
<span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="nx">input</span><span class="o">!==</span><span class="nx">secret</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">count</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">_</span> <span class="k">in</span> <span class="nx">history</span><span class="p">)</span>
<span class="nx">count</span><span class="o">++</span><span class="p">;</span>
<span class="nx">alert</span><span class="p">(</span><span class="s1">'你猜了 '</span><span class="o">+</span><span class="nx">count</span><span class="o">+</span><span class="s1">' 次'</span><span class="p">);</span>
<span class="p">})();</span>
定义个全局变量,记录用户每次输入的值,一样的值不做重复记录,不一样的记录。最后看看这个变量的数。。。。
第一种方法:建个数组,用户输入时检查数组最后一个数是否和输入相等,不相等就push进去。最后输出数组的长度就好了。第二种方法:不管用户输入是否有连续重复的数字,都push到一个数组里,最后把连续的数字剔除掉之后的长度就好了。这样问题就变成了:实现一个函数,用户输入:[1,2,2,2,3,3,4,4,4,5,5],要求输出为5 (即[1,2,3,4,5]的长度)。
<span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">];</span>
<span class="kd">function</span> <span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span><span class="nx">i</span><span class="p">,</span><span class="nx">arr</span><span class="p">){</span>
<span class="k">return</span> <span class="nx">v</span><span class="o">!==</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span>
<span class="p">}).</span><span class="nx">length</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">));</span> <span class="c1">// 5</span>
var n=0;var lastAnswer=null;
//var 答案数组=[];//题主的问题里没有要求记录用户输入的答案,如果有需求的话,就加上这个。
function 用户输入之后的处理函数(int 用户输入的值){
if(lastAnswer!=用户输入的值){
n++;
lastAnswer=用户输入的值;
//答案数组.push(用户输入的值);//题主的问题里没有要求记录用户输入的答案,如果有需求的话,就加上这个。
//和正确答案比较。
}
else{
//连续猜测同一个数字;
}
} 不知道间隔猜测同样的数字算不算你说的连续输入相同的数字 ?不过原理是一样的 :
声明一个数组 ,里面放之前猜测过的数字,每次输入新数字时到数组中查找是否已存在,已经存在的,则跳过。 参考数组去重,很简单实现 把每次数字存入数组,对比数字和数组最后一个数是否相等。 hash虽说不太懂大概意思就是再弄一个数组 专门放这个数字猜没猜过? 这不是很简单么,两个变量的事,一个答案,一个是上次的值

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
