将三个字符串中的所有单词大写

时间:2016-11-20 21:32:22

标签: java string capitalize

我的程序有3个字符串,用户已经输入,此程序的目的是大写单词中的所有字母。如何使用toUpperCase()大写每个单词中的所有字母。这是我到目前为止,这是一个java代码

public static String reverseOrder(String word1, String word2, String word3) {

if (word1.length() == 0) return word1;
    return word1.substring(0, 1).toUpperCase() + word1.substring(1).toUpperCase();
}

我只用了1个单词,但我需要将所有3个单词大写,谢谢 ***确定新代码是这个

public static String reverseOrder(String word1, String word2, String word3) {
  int a = word1.length();
  int b = word2.length();
  int c = word3.length();

  String x;
  String y;
  String z;


  x = word1.toUpperCase();
  y = word2.toUpperCase();
  z = word3.toUpperCase();
}
}

1 个答案:

答案 0 :(得分:0)

你似乎过于复杂化了一个可以用非常简单的方式解决的问题。我们可以将字符串转换为大写,如下所示:

String exampleString = "test";
return exampleString.toUpperCase();

在您的情况下,您有三个字符串。为什么不将它们连接在一起以便将它们转换为一个?例如:

String a = "one";
String b = "two";
String c = "three";

String combined = a + b + c;
return combined.toUpperCase();

如果您能够修改返回类型,则还有其他选项,但这可能不属于问题范围。

相关问题