条件问题以及switch语句

时间:2016-02-03 22:58:50

标签: javascript switch-statement conditional

我正在尝试编写一个程序来显示废水中生物质的产量。一开始我想问一些问题。我希望代码检查输入的内容是否与我拥有的捐赠者/接受者的类型相匹配。我以为!=会检查它们是不是那样。它似乎不起作用

如果删除switch语句的所有内容,代码也会运行。

总结

1)为什么“!=”命令没有程序检查它是否与给定的任何名称都不匹配? 2)我的switch语句是否会导致问题?我正在做一些语法吗?

var donor = prompt("What is your electron donor?");

if (donor != "Acetate" || "Alanine" || "Benzoate" || "Citrate" || "Ethanol" || "Formate" || "Glucose" || "Glutamate" || "Glycerol" || "Glycine" || "Lactate" || "Methane" || "Methanol" || "Palmitate" || "Propionate" || "Pyruvate" || "Succinate") {
  var donor = prompt("What is your electron donor?");
} else {;
}

var acceptor = prompt("What is your electron acceptor?");

if (acceptor != "Ammonium-Nitrate" || "Ammonium-Nitrite" || "Ammonium-Nitrogen" || "Ferrous-Ferric" || "Hydrogen-H+" || "Nitrite-Nitrate" || "Nitrogen-Nitrate" || "Nitrogen-Nitrite" || "Sulfide-Sulfate" || "Sulfide-Sulfite" || "Sulfite-Sulfate" || "Sulfur-Sulfate" || "Thiosulfate-Sulfate" || "Water-Oxygen") {
  var acceptor = prompt("What is your electron acceptor?");
} else {;
}

var nitrogen = prompt("What is your Nitrogen source?");

if (nitrogen != "Ammonium" || "Nitrate") {
  var nitrogen = prompt("What is your Nitrogen source?");
} else {;
}

var carbon = prompt("What is your Carbon source?");


switch (donor) {
  case 'Acetate':
    var Gd = 27.4;
    break;
  case 'Alanine':
    var Gd = 31.37;
    break;
  case 'Benzoate':
    var Gd = 27.34;
    break;
  case 'Citrate':
    var Gd = 33.08;
    break;
  case 'Ethanol':
    var Gd = 31.18
    break;
  case 'Formate':
    var Gd = 39.19
    break;
  case 'Glucose':
    var Gd = 41.35
    break;
  case 'Glutamate':
    var Gd = 30.93
    break;
  case 'Glycerol':
    var Gd = 38.88
    break;
  case 'Glycine':
    var Gd = 39.8
    break;
  case 'Lactate':
    var Gd = 32.29
    break;
  case 'Methane':
    var Gd = 23.53
    break;
  case 'Methanol':
    var Gd = 36.84
    break;
  case 'Palmitate':
    var Gd = 27.26
    break;
  case 'Propionate':
    var Gd = 27.63;
    break;
  case 'Pyruvate':
    var Gd = 35.09;
    break;
  case 'Succinate':
    var Gd = 29.09;
    break;
   default:
    ;

}

switch (acceptor) {
  case 'Ammonium-Nitrate':
    var Ga = -35.11;
    break;
  case :'Ammonium-Nitrite':
    var Ga = -32.93 l
    break;
  case :'Ammonium-Nitrogen':
    var Ga = 26.70;
    break;
  case :'Ferrous-Ferric':
    var Ga = -74.27;
    break;
  case :'Hydrogen-H+':
    var Ga = 39.87;
    break;
  case 'Nitrite=Nitrate':
    var Ga = -41.65;
    break;
  case 'Nitrogen-Nitrate':
    var Ga = -72.2;
    break;
  case 'Nitrogen-Nitrite':
    var Ga = -92.56;
    break;
  case 'Sulfide-Sulfate':
    var Ga = 20.85;
    break;
  case ' Sulfide-Sulfite':
    var Ga = 11.03;
    break;
  case 'Sulfite-Sulfate':
    var Ga = 50.3;
    break;
  case 'Sulfur-Sulfate':
    var Ga = 19.15;
    break;
  case 'Thiosulfate-Sulfate':
    var Ga = 23.58;
    break;
  case 'Water-Oxygen':
    var Ga = -78.72;
    break;
   default:
    ;

}

var trophic = prompt("Is this process Heterotrophic or Autotrophic?")

if (trophic = "Autotrophic") {
  var Gp = 113.8;
} else if (trophic = "Heterotrophic") {
  var Gp = 35.09 - Gd;
} else {
  var trophic = prompt("Is this process Heterotrophic or Autotrophic?");
}


if (nitrogen = 'Ammonium') {
  var Gpc = 18.8;
} else {
  var Gpc = 13.4;
}


var epsilon = prompt("What is your value for epsilon? (Assume 0.6 if you do not know)")

if (Gp > 0) {
  var n = 1
} else {
  var n = -1
}

var Gr = Ga - Gd

var A = -((Gp / (epsilon ^ n)) + (Gpc / epsilon)) / (epsilon * Gr)

var fs = 1 / (1 + A)

var fe = 1 - fs

var Y = fs / 1.42

2 个答案:

答案 0 :(得分:2)

这就是问题所在:

if (donor != "Acetate" || "Alanine" || "Benzoate" || ...

检查donor的值时,您必须检查每个项目。

if (donor != "Acetate" || donor != "Alanine" || donor != "Benzoate" || ...
然而,这仍然是一个笨拙的解决方案。我个人会这样做:

var allowedDonors = "acetate alanine benzonate ...";
if( ~allowedDonors.indexOf(donor) ) { 
  // Code handling a valid donor
}

虽然即使这并不完美,但如果输入与allowedDonors字符串中的拼写完全相同,则会失败,但它是一个开始的地方。

您也不需要空else {}块。

至于您的switch语句(以及其他一些地方),您在代码部分中声明var s,以确定其值。对于某些事情来说这很好,但由于您希望以后使用这些变量,最好在ifswitch语句之外声明它们:

var carbon = prompt("What is your Carbon source?");

var Gd;

switch (donor) {
  case 'Acetate':
    Gd = 27.4;
    break;
  case 'Alanine':
    Gd = 31.37;
    break;

...

通过这种方式,您可以稍后在代码中使用它们(就像您尝试在最底层一样)。

请注意,Javascript中的^运算符不用于指数 - 您需要Math.pow(base, exponent)

在代码中较低,您使用=进行比较,==是赋值运算符。使用if( nitrogen = "Ammonium")比较两个值,例如:if( nitrogen == "Ammonium")应为factoryImpl

答案 1 :(得分:0)

创建一个函数并发送你想要的参数...这样你的开关就会高效100% 例如: 执行所有if语句和代码然后添加;

condorSwitch(condor);
        function condorSwitch(condor){
            //your switch statements code
                }