将数字转换为字符串,以便计算机可以比较它们? [英] Transform numbers into a string so the computer can compare them?

查看:63
本文介绍了将数字转换为字符串,以便计算机可以比较它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS





CSS


body {
   background-color: #90C9C0;
}

h1 {
   font-family: Cambria, sans-serif;
   font-size: 45px;
   color: #FFFFFF;
}

button {
   background-color: #FFFFFF;
   padding: 10px 25px;
}

input[type=text] {
   -webkit-transition: width 20.4s ease-in-out;
   transition: width 20.0s ease-in-out;
   padding: 12px 20px;
   margin: 8px 0;
}

input[type=text]:focus {
   width: 40%;
}





HTML





HTML

<html>
   <body>

   <div style="width: 1333px; height: 62px; background-color: #263035">
      <center>
         <h1>Rock, Paper, or Scissors?</h1></center>
   </div>

   <center>
      

      <br>

      <input type="text" placeholder="Make your choice...">

      <br>

      <button id="user" onclick="Game();"> Good Luck! </button>
   
        </center>

   </body>
</html>





JavaScript





JavaScript

   var rock = 1;
   var paper = 2;
   var scissors = 3;
   var spock = 4;
   var lizard = 5;

   var ComputerChoice = Math.floor(Math.random() * 5 + 1);
   var UserChoice = document.getElementById("user").value;


function Game() {
   
   if (ComputerChoice == 1) {
      ComputerChoice = "rock";
   }
   if (ComputerChoice == 2) {
      ComputerChoice = "paper";
   }
   if (ComputerChoice == 3) {
      ComputerChoice = "scissors";
   }
   
      if (UserChoice == 1) {
      UserChoice = "rock";
   }
   if (UserChoice == 2) {
      UserChoice = "paper";
   }
   if (UserChoice == 3) {
      UserChoice = "scissors";
   }  
   

  if (UserChoice == "1" && ComputerChoice == "1") {
      document.getElementById("user").innerHTML = "<h2>Hmm, we have a tie! I chose </h2>" + ComputerChoice + " <h2>too.</h2>";
   }

      else if 
         (UserChoice == "1" && ComputerChoice == "2") {
         document.getElementById("user").innerHTML = "<h2>You lost! The computer chose</h2>" + ComputerChoice;
      }
            else if 
         (UserChoice == "1" && ComputerChoice == "3") {
         document.getElementById("user").innerHTML = "<h2>You won! The computer chose</h2>" + ComputerChoice;
      }
               else if 
         (UserChoice == "2" && ComputerChoice == "2") {
         document.getElementById("user").innerHTML = "<h2>Hmm, we have a tie! I chose </h2>" + ComputerChoice + " <h2>too.</h2>";
      }
                  else if 
         (UserChoice == "2" && ComputerChoice == "1") {
         document.getElementById("user").innerHTML = "<h2>You won! The computer chose</h2>" + ComputerChoice;
      }
                        else if 
         (UserChoice == "2" && ComputerChoice == "3") {
         document.getElementById("user").innerHTML = "<h2>You lost! The computer chose</h2>" + ComputerChoice;
      }
                              else if 
         (UserChoice == "3" && ComputerChoice == "1") {
         document.getElementById("user").innerHTML = "<h2>You lost! The computer chose</h2>" + ComputerChoice;
      }
                                    else if 
         (UserChoice == "3" && ComputerChoice == "2") {
         document.getElementById("user").innerHTML = "<h2>You won! The computer chose</h2>" + ComputerChoice;
      }
                                      else if 
         (UserChoice == "3" && ComputerChoice == "3") {
         document.getElementById("user").innerHTML = "<h2>Hmm, we have a tie! I chose </h2>" + ComputerChoice + " <h2>too.</h2>";
      } 
                                         else{
      document.getElementById("user").innerHTML = "<h2> I have no idea what you typed... try using words. </h2>";
      }

}





我尝试过:



我尝试将变量重命名为数字然后反之亦然。我的If语句唯一有效的是If和Else Ifs末尾的else语句。是否滥用变量?我使用正确的比较吗?同样,我的按钮仅适用于我的ELSE语句。



What I have tried:

I have tried renaming my variables to numbers and then vise versa. The only thing that works from my If-statements is the "else" statement at the end of the If's and Else Ifs. Is there a misuse of variables? Am I using the correct comparisons? Again, my button is only functioning for my ELSE statement.

推荐答案

您已经多次询问有关此程序的问题。你的问题是如此基本,我想知道你是否打算正常学习JS?

在这里,你将学习JS是如何工作的。

JavaScript教程 [ ^ ]



JS可以完美处理字符串和数字,是您的选择。你只需要了解它是如何工作的。



You have already asked questions multiple time about this program. And your questions are so basic that I wonder if you plan to learn properly JS or not ?
Here, you will learn how JS works.
JavaScript Tutorial[^]

JS can perfectly handle strings and number, your choice. You just have to learn how it works.

if (ComputerChoice == 1) {
   ComputerChoice = "rock";
}



如果您不明白代码失败的原因,请使用浏览器的调试器查看发生的情况。

您应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

有在调试器中没有魔法,它没有找到bug,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


If you don't understand why your code is failing, use your browser's debugger to see what happen.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


它会转到最后一个,为什么?你应该通过向后追踪来做一些侦探工作,匪徒在哪里?它们是:

It will go to the last else, why? You should do some detective work by tracking backwards, where are the culprits? They are:
if (ComputerChoice == 1) {
   ComputerChoice = "rock";
}
if (ComputerChoice == 2) {
   ComputerChoice = "paper";
}
if (ComputerChoice == 3) {
   ComputerChoice = "scissors";
}

   if (UserChoice == 1) {
   UserChoice = "rock";
}
if (UserChoice == 2) {
   UserChoice = "paper";
}
if (UserChoice == 3) {
   UserChoice = "scissors";
}



UserChoice和ComputerChoice的值不再是后续if else语句中预期的数字。


The values of UserChoice and ComputerChoice are no longer in digits which are expected in subsequent if else statements.


这篇关于将数字转换为字符串,以便计算机可以比较它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆