如何使整个短语变成大写和小写,而不仅仅是第一个单词或字母,而是整个短语

时间:2019-02-14 01:36:08

标签: java

我似乎无法将整个词组变成大写或小写,只有第一个单词显示并大写

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        //declarations
        Scanner keyboard = new Scanner(System.in);

        //input section
        System.out.print("Enter Your First Name: ");
        String first = keyboard.next();
        System.out.print("Enter Your Middle Name: ");
        String middle = keyboard.next();
        System.out.print("Enter Your Last Name: ");
        String last = keyboard.next();
        System.out.print("Enter Your Favorite Phrase: ");
        int stringSize;
        String phrase = keyboard.next();
        String upper = phrase.toUpperCase();
        String lower = phrase.toLowerCase();

        //processing
        String initials = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0, 1);
        System.out.println("Your initials are: " + initials);
        System.out.println("Your phrase in all CAPS: " + upper);
        System.out.println("Your phrase in all lower case: " + lower);
    }
}

输出应为:

Enter your first name: Mark
Enter your middle name: Clay
Enter your last name: Dietrich
Enter your favorite saying: Never give up, never surrender!
Your initials are: MCD
Your phrase in all caps: NEVER GIVE UP, NEVER SURRENDER!
Your phrase in all lowercase: never give up, never surrender!

1 个答案:

答案 0 :(得分:1)

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        //declarations
        Scanner keyboard = new Scanner(System.in);

        //input section
        System.out.print("Enter Your First Name: ");
        String first = keyboard.nextLine();
        System.out.print("Enter Your Middle Name: ");
        String middle = keyboard.nextLine();
        System.out.print("Enter Your Last Name: ");
        String last = keyboard.nextLine();
        System.out.print("Enter Your Favorite Phrase: ");
        //int stringSize; // dont think you need this
        String phrase = keyboard.nextLine();
        String upper = phrase.toUpperCase();
        String lower = phrase.toLowerCase();

        //processing
        String initials = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0, 1);
        System.out.println("Your initials are: " + initials);
        System.out.println("Your phrase in all CAPS: " + upper);
        System.out.println("Your phrase in all lower case: " + lower);
    }
}