想使用Java程序找到下面给出的输出?

时间:2017-03-01 08:49:45

标签: java

输入文字:are you ready

输出文字:ArE YoU ReAdY

帮助我找不到的东西。

以下是我正在使用的代码:

import java.util.Scanner;
import java.lang.*;
public class Testdemo{

    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        String result = "";
        System.out.println("enter the string");
        String name =sc.nextLine();
        String[] words = name.split("\\s");
        for(int i=0;i<words.length;i++){
            String med = words[i];
            for(int j=0;j<med.length;j++){
                if(i%2 == 0){
                    result = result + Character.toUpperCase(words[i].charAt(0)) +     words[i].substring(1)    + " ";
                }
                else{
                    result = result + Character.toLowerCase(words[i].charAt(0)) + words[i].substring(1) + "    ";
                }
            }
        }
        System.out.println(result);
    }
}

1 个答案:

答案 0 :(得分:1)

    Scanner sc = new Scanner(System.in);
    StringBuffer result = new StringBuffer();
    System.out.println("enter the string");
    String name = sc.nextLine();
    String[] words = name.split("\\s");
    for (int i = 0; i < words.length; i++) {
        String med = words[i];

        for (int j = 0; j < med.length(); j++) {
            if (j % 2 == 0) {
                result.append(Character.toUpperCase(words[i].charAt(j)));
            } else {
                result.append(Character.toLowerCase(words[i].charAt(j)));

            }
        }
        result.append(" ");
    }
    System.out.println(result);