gpt4 book ai didi

javascript - 在接受用户的回答之前,如何将我的问题显示到控制台?

转载 作者:行者123 更新时间:2023-11-29 17:38:08 25 4
gpt4 key购买 nike

请原谅我这个愚蠢的问题,但我是使用 Javascript 的初学者,我不确定这是因为我正在使用的 IDE(它是 Replit)还是​​ Javascript 中有不同的规则(因为我我对 C# 更熟悉一点,如果我使用它就不会遇到这个问题。

但是,在接受用户输入之前,我不知道如何在窗口中显示输入。例如,我有以下代码是我正在创建的一个小游戏的一部分,我会在其中提出琐事问题。

class Question {
constructor(t, oA, oB, oC, oD, ans) {
this.title = t;
this.optionA = oA;
this.optionB = oB;
this.optionC = oC;
this.optionD = oD
this.answer = ans;
}

displayEntireQuestion() {
console.log(`${this.title}`);
console.log(`A.${this.optionA}`);
console.log(`B.${this.optionB}`);
console.log(`C.${this.optionC}`);
console.log(`D.${this.optionD}`);
}
}

let Round1Questions = [
new Question("What was the most popular language of 2018?", "PHP", "JavaScript", "Ruby", "Python", "Javascript"),
new Question("What year did WW2 end in?", "1945",
"1939", "1978", "1942", "1945")
]

let question1 = Round1Questions[Math.floor(Math.random() * Round1Questions.length)];



question1.displayEntireQuestion();

console.log("Please enter the correct answer");

userAnswer = prompt();

if (userAnswer == question1.answer) {
console.log("Correct!");
} else {
console.log("Sorry, wrong answer.");
}

当我运行它时,它会自动转到提示命令,而不首先显示问题。我怎样才能解决这个问题?我觉得问这个问题很愚蠢,因为我习惯在代码逐行执行的 Visual Studio 中使用 C#。

编辑:即使我在提示方式中加入了“请输入正确答案”,也没有解决问题没有先显示的问题

最佳答案

我相信你的问题是 alert()prompt() 暂停(阻止)脚本的执行,这可能是你不能的原因在 prompt() 之前看不到控制台上的日志。您可以在下一个示例中检查它是否不起作用(您提到的问题):

class Question
{
constructor(t,oA,oB,oC,oD,ans)
{
this.title=t;
this.optionA=oA;
this.optionB=oB;
this.optionC=oC;
this.optionD=oD
this.answer=ans;
}

displayEntireQuestion()
{
console.log(`${this.title}`);
console.log(`A.${this.optionA}`);
console.log(`B.${this.optionB}`);
console.log(`C.${this.optionC}`);
console.log(`D.${this.optionD}`);
}
}

let Round1Questions = [];
Round1Questions.push(new Question("What was the most popular language of 2018?","PHP","JavaScript","Ruby", "Python", "Javascript"));
Round1Questions.push(new Question("What year did WW2 end in?", "1945", "1939", "1978", "1942", "1945"));

let question1 = Round1Questions[Math.floor(Math.random()*Round1Questions.length)];
question1.displayEntireQuestion();
let userAnswer = prompt();
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

可以引用:Why does alert(); run before console.log();了解有关此问题的更多背景信息。但是,可以使用 setTimeout() 对此进行简单的修复:

class Question
{
constructor(t,oA,oB,oC,oD,ans)
{
this.title=t;
this.optionA=oA;
this.optionB=oB;
this.optionC=oC;
this.optionD=oD
this.answer=ans;
}

displayEntireQuestion()
{
console.log(`${this.title}`);
console.log(`A.${this.optionA}`);
console.log(`B.${this.optionB}`);
console.log(`C.${this.optionC}`);
console.log(`D.${this.optionD}`);
}
}

let Round1Questions = [];
Round1Questions.push(new Question("What was the most popular language of 2018?","PHP","JavaScript","Ruby", "Python", "Javascript"));
Round1Questions.push(new Question("What year did WW2 end in?", "1945", "1939", "1978", "1942", "1945"));

let question1 = Round1Questions[Math.floor(Math.random()*Round1Questions.length)];
question1.displayEntireQuestion();
let userAnswer = setTimeout(() => prompt());
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

但是我建议用prompt() 来显示你的问题,像这样:

class Question
{
constructor(t,oA,oB,oC,oD,ans)
{
this.title=t;
this.optionA=oA;
this.optionB=oB;
this.optionC=oC;
this.optionD=oD
this.answer=ans;
}

displayEntireQuestion()
{
return prompt(`${this.title}\nA.${this.optionA}\nB.${this.optionB}\nC.${this.optionC}\nD.${this.optionD}`);
}
}

let Round1Questions = [];
Round1Questions.push(new Question("What was the most popular language of 2018?","PHP","JavaScript","Ruby", "Python", "Javascript"));
Round1Questions.push(new Question("What year did WW2 end in?", "1945", "1939", "1978", "1942", "1945"));

let question1 = Round1Questions[Math.floor(Math.random()*Round1Questions.length)];
let userAnswer = question1.displayEntireQuestion();
console.log(userAnswer);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

关于javascript - 在接受用户的回答之前,如何将我的问题显示到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54937820/

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