请大虾帮我解决一下这两个问题吧!

来源:百度知道 编辑:UC知道 时间:2024/07/06 16:14:13
Write a recursive method with the following signature:
public String mirror(String str)
that returns a copy of the string, followed by all the letters except the last in reverse
order. For example, if given "Thursday", you should return "ThursdayadsruhT".

Write a recursive method with the following signature:
public int sumTo(int n)
that computes the sum of all numbers from 0 to n. For example, if called with the
parameter n = 4, the method should return 10 (since 4 + 3 + 2 + 1 = 10).

用数组和循环啊
public String str(String value)
{
String newstr="";

for(int i=0;i<value.length();i++)
{
newstr=newstr+value.charAt((value.length()-1-i));

}
return value+newstr.substring(1, newstr.length());
}

package com.youngmaster;

public class MethodDemo {

public String mirror(String str) {

int length = str.length();
String temp = "";
for (int i = length - 2; i > -1; i--) {
temp += str.charAt(i);
}

return str += temp;
}

public int sumTo(int n) {
int sum = 0;
for (int i = 0; i <= 4; i++) {

sum += i;
}

return sum;
}

public static void main(String[] args) {

MethodDemo method = new MethodDemo();
String str = "helloworld";
System.out.println(method.mirror(str));
System.out.println(me