From scratch learn Java-4. Use strings for communication, java-4 string

Source: Internet
Author: User

From scratch learn Java-4. Use strings for communication, java-4 string

1. Use strings to store text;

2. Display strings in the program;

3. the string contains special characters;

4. concatenate strings;

5. include variables in strings;

6. compare strings;

7. Judge the length of the string;

Program Credits: displays the list of directors and actors for a movie.

1 package com. jsample; 2 3 public class Credits {// name the Java program as the beginning of the Credits 4 public static void main (String [] args) {// main () BLOCK statement, all functions of the program are completed here 5 // set up from flim information 6 String title = "Sharknado "; // create the variable 7 int year = 2013 used to store information about directors and actors and films; // one of them is an integer variable, and the other is a String variable 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 information18 System. out. println (title + "(" + year + ") \ n" + // long statement, output content 19 "A" + director + "film. \ n "+ // line break \ n is used to wrap 20 role1 +" \ t "+ actor1 +" \ n "+ // tab \ t is used to align 21 role2 + "\ t "+ actor2 +" \ n "+ 22 role3 +" \ t "+ actor3 +" \ n "+ 23 role4 +" \ t "+ actor4 ); 24} // end main () function 25} // end of the program
View Code

Output:

Sharknado (2013)
A Anthony Ferrante film.

Fin Ian Ziering
Mozl Tara Reid
George John Heard
Nova Cassie Scerbo

Program Favorite: completes the word guessing game by comparing two string sizes

 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

Output:

Is Fin's favorite weapon a pool cue?
Answer: false
Change guess's value from pool cue to chainsaw
Answer: true

Program: CreditsHighCase: displays the directors and actors (uppercase) of a movie.

1 package com. jsample; 2 3 public class CreditsHighCase {4 public static void main (String [] args) {// start of the main () BLOCK statement, all functions of the program are completed here 5 // set up from film information 6 String title = "Sharknado "; // create the variable 7 8 int year = 2013 used to store information about directors, actors, and films; // one of them is an integer variable, others are String variables 9 String director = "Anthony Ferrante"; 10 director = director. toUpperCase (); 11 12 String role1 = "Fin"; 13 role1 = role1.toUpperCase (); 14 15 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 information36 System. out. println (title + "(" + year + ") \ n" + // long statement, output content 37 "A" + director + "film. \ n "+ // line break \ n is used to wrap 38 role1 +" \ t "+ actor1 +" \ n "+ // tab \ t is used to align 39 role2 + "\ t "+ actor2 +" \ n "+ 40 role3 +" \ t "+ actor3 +" \ n "+ 41 role4 +" \ t "+ actor4 ); 42} // end main () function 43}
View Code

Output:

Sharknado (2013)
A anthony ferrante film.

FIN IAN ZIERING
Mozl TARA REID
GEORGE JOHN HEARD
NOVA CASSIE SCERBO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.