public class Mystery { public static String mystery(String str, int input) { String result = ""; for (int i = 0; i < str.length() - 1; i++) { if (input == 0) { str = ""; result = str; } if (input == -2) { result = str.substring(2, 4); } if (input == 1) { result = str.substring(0, 1); } if (input == 2) { result = str.substring(0, 2); } if (input == 3) { result = str.substring(2, 3); } if (input == 4) { result = str.substring(3, 4); } } return result; } } Failed 1 tests. Here is one: Testing mystery(String str = "test", int input = 8) failed: Solution returned: "test" Submission returned: ""
In a public class named Mystery, how do I write a static method that takes a String and an int value, and returns a string based on the int value.
These are some possible tests:
Testing mystery(String str = "GW", int input = -3) failed: Solution returned: "GW"
Testing mystery(String str = "es", int input = -3) failed: Solution returned: "es"
Testing mystery(String str = "nPdrLLS", int input = -4) failed: Solution returned: "rLLS"
Testing mystery(String str = "ZoMEPS", int input = -3) failed: Solution returned: "EPS"
Testing mystery(String str = "ULwFTrKGnp", int input = -6) failed: Solution returned: "TrKGnp"
public class Mystery {
public static String mystery(String str, int input) {
String result = "";
for (int i = 0; i < str.length() - 1; i++) {
if (input == 0) {
str = "";
result = str;
}
if (input == -2) {
result = str.substring(2, 4);
}
if (input == 1) {
result = str.substring(1,str.length();
}
if (input == 2) {
result = str.substring(2, str.length());
}
if (input == 3) {
result = str.substring(2, 3);
}
if (input == 4) {
result = str.substring(3, 4);
}
}
return result;
}
}
Get Answers For Free
Most questions answered within 1 hours.