HTML上的Javascript输入和输出

时间:2018-12-09 12:45:38

标签: javascript html

我来自Java领域,我试图重写在Eclipse中创建的Java代码。我想将其集成到html网页。根据用户的回答,我将代码重写为javascript,但仍然无法正常工作。 脚本的目的是处理用户提供的3个变量,并输出5个计算结果。基本上我不确定html和javascript之间的连接是否正确。逻辑本身应该起作用。

 <!DOCTYPE html>
 <html>
 <body>


   Teilnehmer Deutschland: <input type= "text" id= "TNDV">

   Teilnehmer Partner: <input type= "text" id= "TNPV">

   Anzahl Nächte: <input type= "text" id= "NV">

   <button onclick = "berechnung()">test </button>

   <p id="TNBI"></p>
   <p id="TNBE"></p>
   <p id="TNBA"></p>
   <p id="TNBG"></p>
   <p id="FP"></p>


 </body> 

  <script>
  var berechnung = (function () {
   function berechnung() {
   }
   berechnung.main = function (args) {
   var TND = document.getElementById("TNDV").value;
   var TNP = document.getElementById("TNPV").value;
   var N = document.getElementById("NV").value;
    var TNBI;
    var TNBE;
    var TNBA;
    var TNBG;
    var FP;
    if (N >= 4) {
        TNBE = (10 * N);
        TNBI = (8 * N);
        TNBA = (4 * N);
    }
    else {
        TNBE = (10 * N);
        TNBI = (4 * N);
        TNBA = (0);
    }
    if (TNP + TND >= 16) {
        TNBG = (TNBI * (TND - 2) * 1 / 3 + TNBE * (TND - 2) * 2 / 3 +   TNBA * (TNP - 2));
    }
    else if (TNP + TND >= 10) {
        TNBG = (TNBI * (TND - 1) * 1 / 3 + TNBE * (TND - 1) * 2 / 3 + TNBA * (TNP - 1));
    }
    else {
        TNBG = (TNBI * (TND - 1) * 1 / 3 + TNBE * (TND - 1) * 2 / 3 + TNBA * TNP);
    }
    if (TND + TNP >= 16) {
        FP = 4;
    }
    else if (TND + TNP >= 10) {
        FP = 2;
    }
    else {
        FP = 1;
    }
     document.getElementById("TNBI").innerText = `Intern: ${TND}`;
     document.getElementById("TNBE").innerText = `Extern: ${TNP}`;  
     document.getElementById("TNBA").innerText = `Partner: ${N}`;
     document.getElementById("TNBA").innerText = `Freie Plätze: ${FP}`;
     document.getElementById("TNBA").innerText = `Gesamt: ${TNBG}`;

  };
  return berechnung;
  }());
  berechnung["__class"] = "berechnung";
  berechnung.main(null);

   </script>

  </html>

1 个答案:

答案 0 :(得分:1)

因此,JavaScript是用词不当,它与Java绝对无关。如果您试图用JavaScript重写Java代码,则有可能。但是,JavaScript不是一种类型化的语言,因此“字符串”之类的东西什么都没有,并且JavaScript中没有Double。

这是您要完成的工作的最好猜测。

解决方案

function berechnung() {
    var TND = parseInt(document.getElementById("TNDV").value);
    var TNP = parseInt(document.getElementById("TNPV").value);
    var N = parseInt(document.getElementById("NV").value);
    var TNBI, TNBE, TNBA, TNBG, FP;
    if (N >= 4) {
        TNBE = (10 * N);
        TNBI = (8 * N);
        TNBA = (4 * N);
    }
    else {
        TNBE = (10 * N);
        TNBI = (4 * N);
        TNBA = (0);
    }
    if (TNP + TND >= 16) {
        TNBG = (TNBI * (TND - 2) * 1 / 3 + TNBE * (TND - 2) * 2 / 3 + TNBA * (TNP - 2));
    }
    else if (TNP + TND >= 10) {
        TNBG = (TNBI * (TND - 1) * 1 / 3 + TNBE * (TND - 1) * 2 / 3 + TNBA * (TNP - 1));
    }
    else {
        TNBG = (TNBI * (TND - 1) * 1 / 3 + TNBE * (TND - 1) * 2 / 3 + TNBA * TNP);
    }
    if (TND + TNP >= 16) {
        FP = 4;
    }
    else if (TND + TNP >= 10) {
        FP = 2;
    }
    else {
        FP = 1;
    }
    document.getElementById("TNBI").innerText = `Intern: ${TND}`;
    document.getElementById("TNBE").innerText = `Extern: ${TNP}`;
    document.getElementById("TNBA").innerText = `Partner: ${N}`;
    document.getElementById("TNBA").innerText = `Freie Plätze: ${FP}`;
    document.getElementById("TNBA").innerText = `Gesamt: ${TNBG}`;

};
Teilnehmer Deutschland: <input type= "text" id= "TNDV"><br>
Teilnehmer Partner: <input type= "text" id= "TNPV"><br>
Anzahl Nächte: <input type= "text" id= "NV"><br>
<button onclick="berechnung()">results</button>

<p id="TNBI"></p>
<p id="TNBE"></p>
<p id="TNBA"></p>
<p id="TNBG"></p>
<p id="FP"></p>