Java -- JAVAString case

1. Case: User login

Demand:

If the user name and password are known, please use the program to simulate the user login. There are three opportunities in total. After login, give the corresponding prompt

analysis:

(1) If the user name and password are known, just define two strings

(2) Enter the user name and password to log in with the keyboard, and use Scanner to achieve

(3) Compare the user name and password entered on the keyboard with the known user name and password, and give the corresponding prompt. Content comparison of strings, implemented with equals()

(4) Use a loop to implement multiple opportunities. The number of times here is clear. Use the for loop to implement. When the login succeeds, use break to end the loop

2. Case: Traversal string

Demand:

Enter a string on the keyboard, and use the program to traverse the string on the console

analysis:

(1) Enter a string on the keyboard and use Scanner to achieve

(2) Traverse the string. First, you can get every character in the string

(3) Traversing the string, you can first obtain the length of the string

(4) Common format for traversing strings

3. Case: Count the number of characters

Demand:

Enter a string on the keyboard, and count the number of occurrences of uppercase letters, lowercase letters, and numeric characters in the string (other characters are not considered)

analysis:

(1) Enter a string on the keyboard and use Scanner to achieve

(2) To count the number of three types of characters, you need to define three statistical variables with initial values of 0

(3) Traverse the string to get each character

(4) Determine the type of the character, and then add 1 to the statistical variable of the corresponding type

If ch is a character:

Capital letters: ch>='A '&&ch<='Z'

Lower case letters: ch>='a '&&ch<='z'

Number: ch>='0 '&&ch<='9'

(5) Output the number of three types of characters

4. Case: Splicing strings

Demand:

analysis:

(1) Define an array of type int, and use static initialization to complete the initialization of array elements

(2) Define a method to splice the data in the int array into a string according to the specified format for return. The return value type is String, and the parameter list is int [] arr

(3) Traverse the array in the method and splice as required

(4) Call the method and accept the result with a variable

(5) Output results

5. Case: String inversion

Demand:

Define a method to implement string inversion. Enter a string on the keyboard. After calling this method, output the result on the console.

For example, enter abc on the keyboard and output cba

analysis:

(1) Enter a string on the keyboard and use Scanner to achieve it;

(2) Define a method to implement string inversion, return value type String and parameter String s;

(3) In the method, the string is regarded as traversal, and then each obtained character is spliced into a string and returned;

(4) Call the method to accept the result with a variable;

(5) Output results

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us