D11117310葉可棠JavaScript將輸入的字串顛倒輸出

網頁介面

葉可棠輸入字串:

程式碼

 
  <p>葉可棠輸入字串:<input id="in" len="40" type="text" /></p>
<p><input onclick="f()" type="button" value="執行JavaScript" /></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 +"<front size=7>" + c + "</front>";
  document.getElementById("out").innerHTML = b;
}
</script>

vs CODE編輯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字典

葉可棠期中考VS Code編輯Python