1.使用字符串来存储文本;
2.在程序中显示字符串;
3.在字符串中包含特殊的字符;
4.拼接字符串;
5.在字符串中包含变量;
6.比较字符串;
7.判断字符串的长度;
程序Credits:显示一部电影的导演和演员名单
1 packagecom.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 packagecom.jsample;2
3 public classFavourite {4 public static voidmain(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 packagecom.jsample;2
3 public classCreditsHighCase {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