Java "incompatable types" 还没看懂;怎么了?
Java "incompatable types" still not understood; What is wrong?
似乎是正确的,我只是需要帮助找出为什么会给我这个错误。这是提示:
1.Create 一个 MyStr class
2.Write MyStr Class 中称为 break( ) 的方法。
方法 break( ) 接受一个字符串作为其参数,returns 前面最后两个字符的字符串。
这是一个输出示例:
MyStr.break(你好)
returns洛赫尔
Mystr.break(活跃)
returns 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));
}
}
改变
public static void strBreak(String word){
到
public static String strBreak(String word){
因为这个方法returns一个字符串。
关键字void
表示'nothing',它用于声明一个returns什么都没有的方法。如果一个方法 returns 任何东西,你应该在方法声明中指定一个正确的数据类型。
public class MyStr{
public static String strBreak(String word){
int x = word.length() - 2;
return(word.substring(x) + word.substring(0, x));
}
}
以下内容即可。
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 class 没有打印任何结果
- 您的 MyStr 返回的是 void 而不是 String
似乎是正确的,我只是需要帮助找出为什么会给我这个错误。这是提示:
1.Create 一个 MyStr class
2.Write MyStr Class 中称为 break( ) 的方法。 方法 break( ) 接受一个字符串作为其参数,returns 前面最后两个字符的字符串。
这是一个输出示例:
MyStr.break(你好) returns洛赫尔
Mystr.break(活跃) returns 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));
}
}
改变
public static void strBreak(String word){
到
public static String strBreak(String word){
因为这个方法returns一个字符串。
关键字void
表示'nothing',它用于声明一个returns什么都没有的方法。如果一个方法 returns 任何东西,你应该在方法声明中指定一个正确的数据类型。
public class MyStr{
public static String strBreak(String word){
int x = word.length() - 2;
return(word.substring(x) + word.substring(0, x));
}
}
以下内容即可。
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 class 没有打印任何结果
- 您的 MyStr 返回的是 void 而不是 String