我对Java完全陌生,我想知道为什么此函数不打印任何内容。
public class testingStuff {
public String func(){
int i;
String foo = "";
for (i=0; i<3; i++ ){
foo = "The number is "+i + " \n";
}
return foo;
}
public static void main(String[] args)
{
testingStuff stuff = new testingStuff();
stuff.func();
}
}
public class testingStuff
{
public String func()
{
int i;
String foo = "";
for (i=0; i<3; i++ )
{
foo += "The number is "+i + " \n";
}
return foo;
}
public static void main(String[] args)
{
testingStuff stuff = new testingStuff();
String s = stuff.func();
System.out.println(s); //this will print to console.
}
}
或者,如果您更喜欢这样:
public void func()
{
int i;
String foo = "";
for (i=0; i<3; i++ )
{
foo = "The number is " + i;
System.out.println(foo);
}
}
public static void main(String[] args)
{
testingStuff stuff = new testingStuff();
stuff.func();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句