[java程式碼程式庫]-簡易計算機

來源:互聯網
上載者:User

簡易計算機(效果)

  第一種方案:採用Javascript+html完成計算機,支援+-*/,結果顯示不允許使用input輸入欄位(可以考慮使用《span》)

 

<html><head><title>簡易計算機</title><script language="javascript">//易錯:不是"text/javascript"function doCal(){//alert(document.getElementByName("value1").length);//易錯:getElementByName,首字母大寫,不是byName//alert(document.getElementByName("value1")[0].value);//易錯:getElementByName,返回一個集合,應該用[0]表示需要獲得第幾個元素var value1=parseInt(document.getElementById("value1").value);//易錯:易錯:getElementById首字母大寫,不是byIdvar flag=document.getElementById("flag").value;var value2=parseInt(document.getElementById("value2").value);//易錯:getElementBy返回是String類型,應該通過parseInt轉換為Int類型//alert(value1+","+flag+","+value2);//多列印東西出來,用於檢驗var s=0;switch(flag){case "+"://易錯:文法,別忘記加冒號s=value1+value2;break;//易錯:文法,別忘記加break;跳出選擇case "-":s=value1-value2;break;case "*":s=value1*value2;break;case "/":s=value1/value2;}//使運算結果顯示在瀏覽器中,注意=右側為string類型document.getElementById("span_result").innerHTML="<font size='16' color='red'>"+s+"</font>"}//alert(document.getElementByName(value1).value);</script></head><body><h1>簡易計算機</h1><hr><input type="text" name="value1" id="value1"><select name="flag" id="flag">//注意select-option的用法<option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option></select><input type="text" name="value2" id="value2"><!--<button type="button" onclick="doCal()">點擊這裡</button>--><input type="button" value="=" onclick="doCal()">//button的onclick事件<!--<input type="text" name="result" id="result">--><span id="span_result"></span>//通過span顯示結果</body></html>

  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.