D11117311陳瑀晞JavaScript執行輸入字串再反向輸出

網頁介面

陳瑀晞輸入字串:

程式碼

<p>陳瑀晞輸入字串:<input type="text" len="200" 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 "<br>長度是:" + 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);
    }
 
}

留言

這個網誌中的熱門文章

產業分析作業

陳瑀晞python運算CSV輸出TKINTER投資風險與報酬Markowitz

馬可維茲風險平均數平面