英文:
Returning a string that begins at the input parameter index of the String
问题
方法名称是getTheString()
返回类型是String
输入参数是int
我必须返回一个字符串,该字符串从实例变量str
中的输入参数索引位置开始。如果输入参数索引位置大于等于字符串长度,则返回空字符串。
一个示例可能是:
如果str = “Hawaii”
getTheString(8)
返回“”
getTheString(2)
返回
“waii”
我不知道为什么执行时什么都没有发生,或者如何修复它,有人能帮忙吗?
英文:
The Method name is getTheString()
The Return type is String
And the Input parameters is int
I have to return a string that begins at input parameter index position of the String str which is the instance variable. Returns empty string if the input parameter index position is larger than or equal to the string length.
An example would be
If str = “Hawaii”
getTheString(8) returns “ “
getTheString(2) returns
“waii”
I don't know why nothing happens when I execute it or how to fix it could someone help.
public class LoopM
{
public int a, b;
public String str;
public LoopM(){
}
public String getTheString(int f){
str = "Hawaii";
int length = str.length();
for(f = length; f < 10; f++){
return str.substring(f);
}
return str.substring(f);
}
public static void main(String args[]){
//used for testing
LoopM me = new LoopM();
me.getTheString(2);
}
}
答案1
得分: 0
更新为:
public class Test {
public static void main(String[] args) {
System.out.println(getTheString(2));
}
public static String getTheString(int f) {
String str = "Hawaii";
return (f > str.length() || f < 0) ? "" : str.substring(f);
}
}
输出:
waii
英文:
Update to this :
public class Test {
public static void main(String[] args) {
System.out.println(getTheString(2));
}
public static String getTheString(int f) {
String str = "Hawaii";
return (f > str.length() || f < 0) ? "" : str.substring(f);
}
}
Output :
waii
答案2
得分: 0
你应该使用构造函数来设置str
字段的值,而不是在getTheString
方法内部进行硬编码。另外,return
语句内部带有循环有点奇怪。
此外,你应该检查输入参数是否有效(即,如果str
不为null
,并且f
不是负数且小于str.length()
)。
因此,可以按以下方式改进代码:
public class LoopM {
public int a, b;
public String str;
public LoopM() {
this("Hawaii");
}
public LoopM(String str) {
this.str = str;
}
public String getTheString(int f) {
int length = str == null ? -1 : str.length();
return f > -1 && f < length ? str.substring(f) : "";
}
public static void main(String args[]) {
// 用于测试
LoopM me = new LoopM();
System.out.printf("\"%s\"%n", me.getTheString(2)); // 打印输出 "waii"
System.out.printf("\"%s\"%n", me.getTheString(10)); // 打印输出 ""
}
}
英文:
You should be using constructor to set value of str
field instead of hardcoding it inside getTheString
method, also the loop with the return
statement inside is a bit strange.
Also you should check if the input parameters are valid (i.e., if str
is not null
and that f
is not negative and less than str.length()
Thus, the code may be improved as follows:
public class LoopM {
public int a, b;
public String str;
public LoopM() {
this("Hawaii");
}
public LoopM(String str) {
this.str = str;
}
public String getTheString(int f) {
int length = null == str? -1 : str.length();
return f > -1 && f < length ? str.substring(f) : "";
}
public static void main(String args[]){
//used for testing
LoopM me = new LoopM();
System.out.printf("\"%s\"%n", me.getTheString(2)); // prints "waii"
System.out.printf("\"%s\"%n", me.getTheString(10)); // prints ""
}
}
答案3
得分: 0
The return statement consists of a ternary condition similar to if-else conditions. If the index is greater than the length of the string than it will return " " else it will return substring from the input index
public class Test {
public static void main(String[] args) {
System.out.println("String that begins at 2 index : "+getTheString(2)); // Output waii
System.out.println("String that begins at 8 index : "+getTheString(8));// Output ""
}
public static String getTheString(int index) {
String str = "Hawaii";
return index > str.length() ? "" : str.substring(index);
}
}
英文:
The return statement consists of a ternary condition similar to if-else conditions. If the index is greater than the length of the string than it will return " " else it will return substring from the input index
public class Test {
public static void main(String[] args) {
System.out.println("String that begins at 2 index : "+getTheString(2)); // Output waii
System.out.println("String that begins at 8 index : "+getTheString(8));// Output ""
}
public static String getTheString(int index) {
String str = "Hawaii";
return index > str.length() ? "" : str.substring(index);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论