2016-11-28 101 views
-3

这是我的方法。它不会返回任何东西。请帮助,我不知道如何获得所需的字符串名称更长(字符之间带连字符的初始字符串)。 `如何在字符串中的字符之间添加连字符,创建新字符串作为结果

public static String stretch(String word){ 

    String longer = "" + word.charAt(0); 

    for (int i=1; i<=word.length()-1; i++){ 
     longer += "-" + word.charAt(i); 
    } 

    return longer; 
} 
+1

咦?不知道你在问什么。请显示预期的输入和输出。谢谢。 – OldProgrammer

+1

你想运行'stretch(“sample string”)'并返回''s-a-m-p -l-e- -s-t -r-i-n -g“'? – Tim

回答

1
class Main { 
    public static void main(String[] args) { 
    System.out.println(stretch("test")); 
    } 

    public static String stretch(String word){ 
    String longer = "" + word.charAt(0); 
     for (int i=1; i<=word.length()-1; i++){ 
     longer += "-" + word.charAt(i); 
     } 
     return longer; 
    } 
} 

输出:

t-e-s-t 

试试吧here!

相关问题