-2

似乎它是正确的,我只需要帮助找出它为什么给我这个错误。这是提示:

1.创建一个MyStr类

2. 在 MyStr 类中编写一个名为 break() 的方法。break() 方法接受一个字符串作为其参数,并返回最后两个字符在前面的字符串。

以下是输出示例:

  1. MyStr.break(Hello) 返回 loHel

    Mystr.break(Active) 返回 veActi

到目前为止,这是我的代码:

public class Main {
  public static void main(String[] args) {
    MyStr.strBreak("Morse");
    MyStr.strBreak("School");
  }
}

public class MyStr{
  public static void strBreak(String word){
    int x = word.length() - 2;
    return(word.substring(x) + word.substring(0, x));
  }
}
4

3 回答 3

2

改变

public static void strBreak(String word){

public static String strBreak(String word){

因为这个方法返回一个字符串。

关键字void表示“无”,它用于声明不返回任何内容的方法。如果方法返回任何内容,则应在方法声明中指定正确的数据类型。

于 2019-12-01T23:24:15.537 回答
0
public class MyStr{ 
    public static String strBreak(String word){ 
        int x = word.length() - 2; 
        return(word.substring(x) + word.substring(0, x)); 
    } 
}
于 2019-12-01T23:25:04.020 回答
0

下面会做。

    public class Main {
  public static void main(String[] args) {
    String str=MyStr.strBreak("Morse");
    MyStr.strBreak("School");
    System.out.println(str);
  }
}

public class MyStr{
  public static String strBreak(String word){
    int x = word.length() - 2;
    return(word.substring(x) + word.substring(0, x));
  }
}

这打印出来-

seMor
  • 您的 Main 类没有打印任何结果
  • 您的 MyStr 返回 void 而不是 String
于 2019-12-01T23:25:40.237 回答