在Java中反转一个句子

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

Reverse a sentence in Java

问题

public class backwards
{
    public static void main(String args[])
    {
        String s1 = new String("pay no attention to that man behind the curtain");

        int pos = 0;
        for(int i = s1.length()-1 ; i >= 0; i--)
        {
            if(s1.charAt(i) == ' ')
            {
                System.out.println(s1.substring(i+1));
                s1 = s1.substring(0,i);
            }
            else if(i == 0)
            {
                System.out.println(s1);
                s1 = "";
            }
        }
    }
}
英文:

> The question for homework is the program should print the String in reverse word by word.<br>
> Your `String' should be assigned the value “pay no attention to that man behind the curtain” and should be printed as the sample output.

Getting errors compiling and been at this for 3 hours - lost!!

I must use the charAt method, the substring method and an if statement:

curtain
the
behind
man
that
to
attention
no
pay

public class backwards
{
	public static void main(String args[])
	{
		String s1 = new String(&quot;pay no attention to that man behind the curtain&quot;);

		/*int pos = s1.indexOf(&#39; &#39;);
		while(s1.length() &gt;  0)
		{
			if(pos == -1)
			{
				System.out.println(s1);
				s1 = &quot;&quot;;

			}
			else
			{
				System.out.println(s1.substring(0,pos));
				s1 = s1.substring(pos+1);
				pos = s1.indexOf(&#39; &#39;);
			}

		}*/
		int pos = 0;
		for(int i = s1.length()-1 ; i &gt;= 0; i--)
		{
		//	System.out.println(&quot;Pos: &quot; + pos);
			if(s1.charAt(i) == &#39; &#39;)
			{
				System.out.println(s1.substring(i+1));
				s1 = s1.substring(0,i);
			}
			else if(i == 0)
			{
				System.out.println(s1);
				s1 = &quot;&quot;;
			}
		}
	}
}

答案1

得分: 3

你可以简单地这样做:

public class Main {
    public static void main(String[] args) {
        // 按空白字符分割
        String[] arr = "pay no attention to that man behind the curtain".split("\\s+");

        // 以相反顺序打印数组
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.println(arr[i]);
        }
    }
}

输出:

curtain
the
behind
man
that
to
attention
no
pay

**或者,**可以这样做:

public class Main {
    public static void main(String[] args) {
        String s1 = "pay no attention to that man behind the curtain";
        for (int i = s1.length() - 1; i >= 0; i--) {
            if (s1.charAt(i) == ' ') {
                // 打印 `s1` 的最后一个单词
                System.out.println(s1.substring(i + 1));

                // 去掉最后一个单词并将剩余的字符串赋给 `s1`
                s1 = s1.substring(0, i);
            } else if (i == 0) {
                // 如果 `s1` 只剩下一个单词
                System.out.println(s1);
            }
        }
    }
}

输出:

curtain
the
behind
man
that
to
attention
no
pay
英文:

You can do it simply as

public class Main {
	public static void main(String[] args) {
		// Split on whitespace
		String[] arr = &quot;pay no attention to that man behind the curtain&quot;.split(&quot;\\s+&quot;);

		// Print the array in reverse order
		for (int i = arr.length - 1; i &gt;= 0; i--) {
			System.out.println(arr[i]);
		}
	}
}

Output:

curtain
the
behind
man
that
to
attention
no
pay

Alternatively,

public class Main {
	public static void main(String[] args) {
		String s1 = &quot;pay no attention to that man behind the curtain&quot;;
		for (int i = s1.length() - 1; i &gt;= 0; i--) {
			if (s1.charAt(i) == &#39; &#39;) {
				// Print the last word of `s1`
				System.out.println(s1.substring(i + 1));

				// Drop off the last word and assign the remaining string to `s1`
				s1 = s1.substring(0, i);
			} else if (i == 0) {
				// If `s1` has just one word remaining
				System.out.println(s1);
			}
		}
	}
}

Output:

curtain
the
behind
man
that
to
attention
no
pay

huangapple
  • 本文由 发表于 2020年9月12日 05:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63854816.html
匿名

发表评论

匿名网友

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

确定