java字符串s-director_从零自学Java-4.使用字符串来交流

1.使用字符串来存储文本;

2.在程序中显示字符串;

3.在字符串中包含特殊的字符;

4.拼接字符串;

5.在字符串中包含变量;

6.比较字符串;

7.判断字符串的长度;

程序Credits:显示一部电影的导演和演员名单

e2b97a2a8e5a785b344cebe353f29e1d0df.jpg

094ecd22593e946ecc17cffd8a02010a7ad.jpg

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:通过比较两个字符串大小来完成猜词游戏

9b6c539901a4b861977efa3f4a47932f20c.jpg

99e916e4ee4733042c99ab81a9a7365869f.jpg

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:显示一部电影的导演和演员(大写)名单

7758646cecfa9b0790aaff277159383dc21.jpg

a7be1634cdeae0e5e43fcea96197b60bd32.jpg

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值