JavaScript將輸入的字串顛倒輸出
網頁介面
廖定宏輸入字串:
程式碼<style>h1{background-color: yellow;}</style><h1>網頁介面</h1><p>廖定宏輸入字串:<input id="in" len="50" 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 + "<font size=7>" + c + "</font>"; 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);
}
}
留言
張貼留言