gpt4 book ai didi

javascript - Node.js:telnet "Rock, Paper, Scissors"程序无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 02:46:34 25 4
gpt4 key购买 nike

所以我正在尝试创建这个石头、剪刀、布程序来在 telnet 服务器上运行。但它不允许我输入“石头”、“布”或“剪刀”等词。

  1. 第一个 CPUhand 显示为未定义,并且未设置为我的 if 语句中的选项之一。
  2. 其次,每当我在命令提示符中输入单个字符时,它都会显示“无效,请重试!” else 语句并跳到下一行。

有谁能够弄清楚为什么我的 CPUhand if 语句不起作用或者为什么我无法在命令提示符中输入多个字符?

Screenshot1 Screenshot2

"use strict"; 
const
net = require('net'),
server = net.createServer(function(connection) { //create server

let randomNum = randomNumber(1, 3);
let CPUhand

if(randomNum == 1){
CPUhand == "Rock";
} else if(randomNum == 2){
CPUhand == "Paper";
} else if(randomNum == 3){
CPUhand == "Scissors";
}

connection.write("Enter: Rock, Paper, or Scissors!\r\n");

connection.on('data', function(chunk) { //collect data from user

let USERhand = chunk.toString();

if(CPUhand === "Rock" && USERhand === "Scissors" || USERhand === "Rock" && CPUhand === "Scissors"){
connection.write("Rock beats Scissors!\r\n");
}
else if(CPUhand === "Paper" && USERhand === "Rock" || USERhand === "Paper" && CPUhand === "Rock"){
connection.write("Paper beats Rock!\r\n");
}
else if(CPUhand === "Scissors" && USERhand === "Paper" || USERhand === "Scissors" && CPUhand === "Paper"){
connection.write("Scissors beats Paper!\r\n");
}
else if(CPUhand === USERhand){
connection.write("Draw!\r\n");
}
else{
connection.write("Invalid! Try Again! \r\n");
}

});

}); server.listen(5432); //bind port

function randomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

最佳答案

1) block 中的拼写错误:

if(随机数 == 1){
CPUhand == "摇滚";
} 否则...

应该是:

if(随机数 == 1){
CPUhand =“摇滚”;
} 否则...

2)在用户输入中,您还有换行符:

让 USERhand = chunk.toString();

替换为:

让 USERhand = chunk.toString().trim();

关于javascript - Node.js:telnet "Rock, Paper, Scissors"程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com