返回一个从输入参数索引开始的字符串。

huangapple go评论73阅读模式
英文:

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 = &quot;Hawaii&quot;;

		return (f &gt; str.length() || f &lt; 0) ? &quot;&quot; : 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(&quot;Hawaii&quot;);
    }

    public LoopM(String str) {
        this.str = str;
    }
   
   public String getTheString(int f) {
       
       int length = null == str? -1 : str.length();
       
       return f &gt; -1 &amp;&amp; f &lt; length ? str.substring(f) : &quot;&quot;;
    }
   
   public static void main(String args[]){
       //used for testing

       LoopM me = new LoopM();
       System.out.printf(&quot;\&quot;%s\&quot;%n&quot;, me.getTheString(2));  // prints &quot;waii&quot;
       System.out.printf(&quot;\&quot;%s\&quot;%n&quot;, me.getTheString(10));  // prints &quot;&quot;
    }
}

答案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(&quot;String that begins at 2 index : &quot;+getTheString(2)); // Output waii
        System.out.println(&quot;String that begins at 8 index : &quot;+getTheString(8));// Output &quot;&quot;
    }

    public static String getTheString(int index) {

        String str = &quot;Hawaii&quot;;

        return index &gt; str.length() ? &quot;&quot; : str.substring(index);
    }

}

huangapple
  • 本文由 发表于 2020年9月24日 02:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64033870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定