在计算即时答案时遇到问题

时间:2018-07-31 13:51:39

标签: javascript

因此,我试图为用户创建一个提示,以手动输入分数。问题是,所涉及的数学无法正常运行,并将该问题视为乘法问题而不是总和问题。知道我是否有错误或错误的地方吗?这是我的整个代码:

//Code Challenge 02

//Johns team will start on the calculation. 

johnTeam = 89 + 120 + 103;
newScore = johnTeam/3;
johnTeam = newScore;

//For the sake of mathematic sanity, lets put newScore to 0.
newScore = 0;

console.log(johnTeam + " scored this much in average.\n");

//Next is Mikes Team

mikeTeam = 116 + 94 + 123;
newScore = mikeTeam/3;
mikeTeam = newScore;

//Reset newScore
newScore = 0;

console.log(mikeTeam + " scored this much in average.\n");

//Lastly, Mary's Team

maryTeam = 97 + 134 + 105;
newScore = maryTeam/3;
maryTeam = newScore;

//Reset newScore
newScore = 0;

console.log(maryTeam + " scored this much in average.\n");

//Calculate on who won.
console.log("Round 1: Who won?\n");

//Lets make a big huge enormous if and else statements revolving around these teams.
if(johnTeam > mikeTeam && johnTeam > maryTeam)
{
    console.log("Johns team wins!");
}else if(mikeTeam > johnTeam && mikeTeam > maryTeam){
    console.log("Mikes Team wins!");
}else if(maryTeam > johnTeam && maryTeam > mikeTeam){
    console.log("Mary's Team wins!");
}else if(johnTeam === mikeTeam && johnTeam > maryTeam){
    console.log("John and Mike both tied!");
}else if(johnTeam === maryTeam && johnTeam > mikeTeam){
    console.log("John and Mary have both tied!");
}else if(mikeTeam === maryTeam && mikeTeam > johnTeam){
    console.log("Mike and Mary have both tied!");
}else{
    console.log("Everybody is a winner! Wowzerz!");
}

// ********************************************* ******     //现在结束了,准备进行第二轮了吗?让我们尝试一些更现实的方法,而不是那么普遍。

//Reset all scores!

johnTeam = 0;
mikeTeam = 0;
maryTeam = 0;

console.log("\nInsert Johns score per question.");
var a = prompt("Insert first score.");
console.log(a);
var b = prompt("Insert second score,");
console.log(b);
var c = prompt("Insert third score.");
console.log(c);

johnTeam = a + b + c;
newScore = johnTeam/3;
johnTeam = newScore;

newScore = 0;

console.log(johnTeam + " scored this much in average.");

`

目标:要累加三个分数,然后除以平均分数。然后确定哪个队得分更高。

问题:第一部分很好。迅速的部分将方程视为乘法问题。

2 个答案:

答案 0 :(得分:0)

prompt()函数返回一个字符串,因此您必须像这样解析它:

parseInt('your string', 10)

答案 1 :(得分:0)

Prompt返回一个字符串,需要将结果解析为整数以对它们进行数学运算:

  

结果是一个字符串,其中包含用户输入的文本,或者为null。

johnTeam = 0;
mikeTeam = 0;
maryTeam = 0;
console.log("\nInsert Johns score per question.");
johnTeam = parseInt(prompt("Insert first score."));
console.log(johnTeam);
mikeTeam = parseInt(prompt("Insert second score,"));
console.log(mikeTeam);
maryTeam = parseInt(prompt("Insert third score."));
console.log(maryTeam);
newScore = (maryTeam + mikeTeam + johnTeam) / 3
console.log(newScore + " scored this much in average.");