Could anyone please help me identify what's wrong with my code to this instruction here: http://prntscr.com/1w02ns I'm not sure what's quite wrong. Most of the tests in my blueJ project are passing but, some arent.
任何人都可以请帮助我确定我的代码在这里的说法有什么问题:http://prntscr.com/1w02ns我不确定什么是错的。我的blueJ项目中的大多数测试都在通过,但有些不是。
My code so far here:
我的代码到目前为止:
String prefix (String n, int num)
{
int count = 0 ;
for(int i = 0; i <n.length(); i++)
{
if(n.charAt(i) == num)
{
++count;
}
}
return n;
}
5 个解决方案
#1
0
In your case variable count
is not used.
在您的情况下,不使用变量计数。
If you really want to use a loop, not a substring, you can try something like:
如果你真的想要使用循环,而不是子字符串,你可以尝试类似的东西:
String prefix(String str, int n) {
String result = "";
for (int i = 0; i < n; i++) {
result += str.toCharArray()[i];
}
return result;
}
#2
3
Why are you using a loop? Your assignment just states that you need to accept a String and a number.
你为什么要使用循环?你的作业只是声明你需要接受一个字符串和一个数字。
You can just do:
你可以这样做:
public String prefix(String text, int characters) {
return text.substring(0, characters);
}
Reference: toString javadoc
参考:toString javadoc
Edit: Why is it failing, and how is it determined? Are there any requirements/rules that we may need to know? The following JUnit test works fine:
编辑:为什么它失败了,它是如何确定的?我们可能需要了解哪些要求/规则?以下JUnit测试工作正常:
@Test
public void testPrefix() {
Assert.assertEquals("hel", prefix("hello", 3));
}
#3
1
you can simply use the substring function provided in java-string API.There are two version of this substring you can use the 2nd one. below is the link
你可以简单地使用java-string API中提供的substring函数。这个子字符串的两个版本可以使用第二个。以下是链接
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
http://www.tutorialspoint.com/java/java_string_substring.htm
#4
0
Why loop man? use substring na??
为什么要循环人?使用子串na ??
public class Prefix {
public static void main(String args[]){
String r = prefix("Why loop? use substring. Well unless your homework says use loop" , 4);
System.out.println(r);
}
private static String prefix(String string, int i) {
// TODO Auto-generated method stub
return string.substring(0,i);
}
}
#5
0
You can simple use :
你可以简单地使用:
static String prefix(String n, int num) throws Exception {
if (n != null && n.length() < num)
throw new Exception();
return n.substring(0, num);
}
#1
0
In your case variable count
is not used.
在您的情况下,不使用变量计数。
If you really want to use a loop, not a substring, you can try something like:
如果你真的想要使用循环,而不是子字符串,你可以尝试类似的东西:
String prefix(String str, int n) {
String result = "";
for (int i = 0; i < n; i++) {
result += str.toCharArray()[i];
}
return result;
}
#2
3
Why are you using a loop? Your assignment just states that you need to accept a String and a number.
你为什么要使用循环?你的作业只是声明你需要接受一个字符串和一个数字。
You can just do:
你可以这样做:
public String prefix(String text, int characters) {
return text.substring(0, characters);
}
Reference: toString javadoc
参考:toString javadoc
Edit: Why is it failing, and how is it determined? Are there any requirements/rules that we may need to know? The following JUnit test works fine:
编辑:为什么它失败了,它是如何确定的?我们可能需要了解哪些要求/规则?以下JUnit测试工作正常:
@Test
public void testPrefix() {
Assert.assertEquals("hel", prefix("hello", 3));
}
#3
1
you can simply use the substring function provided in java-string API.There are two version of this substring you can use the 2nd one. below is the link
你可以简单地使用java-string API中提供的substring函数。这个子字符串的两个版本可以使用第二个。以下是链接
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
http://www.tutorialspoint.com/java/java_string_substring.htm
#4
0
Why loop man? use substring na??
为什么要循环人?使用子串na ??
public class Prefix {
public static void main(String args[]){
String r = prefix("Why loop? use substring. Well unless your homework says use loop" , 4);
System.out.println(r);
}
private static String prefix(String string, int i) {
// TODO Auto-generated method stub
return string.substring(0,i);
}
}
#5
0
You can simple use :
你可以简单地使用:
static String prefix(String n, int num) throws Exception {
if (n != null && n.length() < num)
throw new Exception();
return n.substring(0, num);
}