【王道JAVA】【程序 38 求字符串長度】

題目:寫一個函數,求一個字符串的長度,在 main 函數中輸入字符串,並輸出其長度。

import java.util.Scanner;

public class WangDao {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.print("Input the string: ");
		String str = scan.nextLine();
		char[] ch = str.toCharArray();
		int n = ch.length;
		System.out.println("The length of the string is " + n);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章