我是java的初学者,我们被要求将这个词的第一个和最后一个字母大写。
[提示:将字母捕获为字符串(使用子字符串方法),然后使用toUpperCase()方法。]
到目前为止我所拥有的只是这个,import java.util.Scanner;
public class Excercise6 {
public static void main (String [] args){
Scanner keyboard = new Scanner (System.in);
System.out.print("Please Type a word: ");
String word = keyboard.nextLine();
int stringLength = word.length();
String letter1 = word.substring (0,1);
String lastletter = word.substring ((stringLength-1),(stringLength));
String newWord =letter1.toUpperCase() + lastletter.toUpperCase();
System.out.println("The word with first letter capitalized is: " + newWord );
}
}
如何在第一个和最后一个字母之间添加单词?
答案 0 :(得分:2)
代码看起来很好,只需进行一些更改
String newWord = letter1.toUpperCase()
+ word.substring(1, word.length() - 1)
+ lastletter.toUpperCase();
第一封信 - letter1.toUpperCase()
中间字符串 - word.substring(1, word.length() - 1)
最后一封信 - lastletter.toUpperCase();
<强>输出强>
Please Type a word: ankur
The word with first letter capitalized is: AnkuR
答案 1 :(得分:1)
尝试这样的事情:
String a = "java";
String first = a.substring(0, 1).toUpperCase();
String last = a.substring((a.length() - 1), a.length()).toUpperCase();
String middle = a.substring(1, a.length() - 1);
System.out.println(first + middle + last);
答案 2 :(得分:0)
您可以尝试以下
String letter1 = word.substring (0,1);
String lastletter = word.substring ((stringLength-1),(stringLength));
String newWord =letter1.toUpperCase() + word.subString(1, stringLength-1) + lastletter.toUpperCase();
这应该给你这个词的“中间”,没有第一个和最后一个字母
答案 3 :(得分:0)
String。substring()方法将为您提供子字符串。因此,将这个词分成几部分,就像你已经部分地做了一样: -
String startChar = word.substring(0, 1);
String endChar = word.substring(word.length()-1); //If you leave out the last argument, it goes all the way to the end of the String
String middle = word.substring(1, word.length()-1);
然后只需构建你的新单词(第一个字符,加上中间部分,加上最后一个字符),并使用字符串转换大写的相关部分。upperCase()方法
String newWord = startChar.toUpperCase() + middle + endChar.toUpperCase();
答案 4 :(得分:0)
你可以简单地做
String st="singhakash";
System.out.println(st.substring(0,1).toUpperCase()+st.substring(1,st.length()-1)+st.substring(st.length()-1).toUpperCase());
输出
SinghakasH
答案 5 :(得分:0)
因为每个人都提出了可行的建议;我正在调整一个更有效和更安全的建议。
String word = keyboard.nextLine();
这不保证一个字。它会返回一行,这意味着在换行符之前的任何内容&#39; \ n&#39;。
要获得这些词语,您应该使用:
String[] words = keyboard.nextLine().split(new char[] { ' ', '\t', '\r' });
这将分割字符的行:空格,制表符和回车符(注意:不是换行符)。
接下来,我们需要使用for-each循环遍历每个单词:
for(String word : words)
{
int wordLength = word.length(); // Get length, because we're gonna use it multiple times
if(wordLength > 0) // Ignore empty words
{
// Only get the first character and convert it to uppercase (better than using substring)
// Also note that we don't have to check if there's a character at 0, because we already did with wordLength > 0
char firstCharacter = Character.toUpperCase(word.charAt(0));
StringBuilder builder = new StringBuilder(firstCharacter); // We use StringBuilder class because it is efficient in concatenating objects to a string
if(wordLength == 2) // We only got 2 characters, so no inbetween
{
char lastCharacter = Character.toUpperCase(word.charAt(1)); // We know this character is at index 1
builder.append(lastCharacter); // Append last character
}
else // More than 2
{
String inbetweenCharacters = word.substring(1, wordLength - 2); // Get the words in between using substring
char lastCharacter = Character.toUpperCase(word.charAt(wordLength - 1)); // Get the last character
builder.append(inbetweenCharacters); // Append inbetween characters
builder.append(lastCharacter); // Append last character
}
System.out.println(builder.toString()); // Print complete converted word
}
}
我做到了这一点,我希望它没有任何拼写错误
答案 6 :(得分:0)
/ **代码添加字符串句子的第1个到第1个,第2个到第2个到第3个到第3个和第3个的字 作者:deependra singh patel * /
import java.util.Scanner;
public class ReverseStringWordConcate {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter The Sentence");
String s1 = scn.nextLine();
String []s2 = s1.split(" ");
int i1 = 0;
for(int i =s2.length-1;i>=0;i--) {
if(s2[i]==s2[i1] ){
char [] arr = s2[i].toCharArray();
System.out.print(arr.length+s2[i1]);
break;
}
else {
//System.out.print(s2[i]+s2[i1]+" ");
char [] arr = s2[i].toCharArray();
char arr1[] = s2[i1].toCharArray();
System.out.print((arr.length+arr1.length)+s2[i]+s2[i1]+" ");
}
i1++;
}
}
}
/ *样本输入:deependra singh patel thegun singhraur
expected output :18singhraurdeependra 11thegunsingh 5patel
* /