1.使用字元串來存儲文本; 2.在程式中顯示字元串; 3.在字元串中包含特殊的字元; 4.拼接字元串; 5.在字元串中包含變數; 6.比較字元串; 7.判斷字元串的長度; 程式Credits:顯示一部電影的導演和演員名單 1 package com.jsample; 2 3 public class ...
1.使用字元串來存儲文本;
2.在程式中顯示字元串;
3.在字元串中包含特殊的字元;
4.拼接字元串;
5.在字元串中包含變數;
6.比較字元串;
7.判斷字元串的長度;
程式Credits:顯示一部電影的導演和演員名單
1 package com.jsample; 2 3 public class Credits {//將該Java程式命名為Credits 4 public static void main(String[] args){//main()塊語句的開頭,程式所有功能都在這裡完成 5 //set up from flim information 6 String title = "Sharknado";//創建用於存儲導演和演員以及影片信息的變數 7 int year = 2013; //其中一個是整型變數,其他都是字元串變數 8 String director = "Anthony Ferrante"; 9 String role1 = "Fin"; 10 String actor1 = "Ian Ziering"; 11 String role2 = "April"; 12 String actor2 = "Tara Reid"; 13 String role3 = "George"; 14 String actor3 = "John Heard"; 15 String role4 = "Nova"; 16 String actor4 = "Cassie Scerbo"; 17 //display information 18 System.out.println(title + " (" + year +")\n" + //長語句,輸出內容 19 "A " + director + " film.\n\n" + //換行符\n用於換行 20 role1 + "\t" + actor1 + "\n" + //製表符\t用於對齊 21 role2 + "\t" + actor2 + "\n" + 22 role3 + "\t" + actor3 + "\n" + 23 role4 + "\t" + actor4); 24 }//結束main()函數 25 }//程式結束View Code
輸出:
Sharknado (2013)
A Anthony Ferrante film.
Fin Ian Ziering
April Tara Reid
George John Heard
Nova Cassie Scerbo
程式Favorite:通過比較兩個字元串大小來完成猜詞游戲
1 package com.jsample; 2 3 public class Favourite { 4 public static void main(String[] args){ 5 String favorite = "chainsaw"; 6 String guess = "pool cue"; 7 System.out.println("Is Fin's favorite weapon a " + guess + "?"); 8 System.out.println("Answer: " + favorite.equals(guess)); 9 System.out.println("Change guess's value from pool cue to chainsaw"); 10 guess = "chainsaw"; 11 System.out.println("Answer: " + favorite.equals(guess)); 12 } 13 }View Code
輸出:
Is Fin's favorite weapon a pool cue?
Answer: false
Change guess's value from pool cue to chainsaw
Answer: true
程式:CreditsHighCase:顯示一部電影的導演和演員(大寫)名單
1 package com.jsample; 2 3 public class CreditsHighCase { 4 public static void main(String[] args){//main()塊語句的開頭,程式所有功能都在這裡完成 5 //set up from film information 6 String title = "Sharknado";//創建用於存儲導演和演員以及影片信息的變數 7 8 int year = 2013; //其中一個是整型變數,其他都是字元串變數 9 String director = "Anthony Ferrante"; 10 director = director.toUpperCase(); 11 12 String role1 = "Fin"; 13 role1 = role1.toUpperCase(); 14 15 String actor1 = "Ian Ziering"; 16 actor1 = actor1.toUpperCase(); 17 18 String role2 = "April"; 19 role2 = role2.toUpperCase(); 20 21 String actor2 = "Tara Reid"; 22 actor2 = actor2.toUpperCase(); 23 24 String role3 = "George"; 25 role3 = role3.toUpperCase(); 26 27 String actor3 = "John Heard"; 28 actor3 = actor3.toUpperCase(); 29 30 String role4 = "Nova"; 31 role4 = role4.toUpperCase(); 32 33 String actor4 = "Cassie Scerbo"; 34 actor4 = actor4.toUpperCase(); 35 //display information 36 System.out.println(title + " (" + year +")\n" + //長語句,輸出內容 37 "A " + director + " film.\n\n" + //換行符\n用於換行 38 role1 + "\t" + actor1 + "\n" + //製表符\t用於對齊 39 role2 + "\t" + actor2 + "\n" + 40 role3 + "\t" + actor3 + "\n" + 41 role4 + "\t" + actor4); 42 }//結束main()函數 43 }View Code
輸出:
Sharknado (2013)
A ANTHONY FERRANTE film.
FIN IAN ZIERING
APRIL TARA REID
GEORGE JOHN HEARD
NOVA CASSIE SCERBO