D11117232胡鈺筠JavaScript執行輸入字串再反向輸出JS Code編輯

h1{border:red}

輸出結果

胡鈺筠輸入字串:

Java程式碼

<p>輸入字串:<input type="text" len="50" id="in"></p>
<p><input type="button" value="執行JavaScript" onclick="f()"></p>
<p id="out"></p>
<script>
function f(){
  var a = document.getElementById("in").value;/*取得id=in的文字*/
  var b = "長度是:" + a.length + ",反串是: ";
  var c = "";
  for (var i = 0; i < a.length; i++)
    c = a.slice(i,i+1) + c ;
  b = b + c;
  document.getElementById("out").innerHTML = b;
}
</script>


Java程式碼

import java.util.Scanner; /*開啟套件package util=utility用途,Scanner掃描器*/ /*胡鈺筠utility industry=公用事業產業,電力,自來水,效用=utility */ class MyClass { public static void main(String[] args) { String a, b=""; /*定義字串a,b */ Scanner myObj = new Scanner(System.in); /*建構掃描物件*/ System.out.print("輸入: "); a = myObj.nextLine(); /*輸入文字nextLine到變數a */ System.out.println("長度: " + a.length());/*輸出字串長度length() */ for (int i = 0; i < a.length(); i++) /*迴圈 */ b = a.charAt(i) + b; /*a字元順序放到b前面*/ System.out.println("輸出: "+ b); } }

留言

這個網誌中的熱門文章

胡鈺筠期貨分析人員2025年第一次衍生性商品風險管理

風險管理期中考作業:風險管理制度與實務18屆考題