2013年11月19日星期二

jsp how to call java class array of classes and methods

My class file
package javabean;
public class HelloWord {

public static void main (String [] args) {
/ / TODO Auto-generated method stub
System.out.print (" Hello , JAVA!");
HelloWord.sayOK ();
System.out.println (HelloWord.getHello ());
int a [] = null;
a = new int [10];
for (int i = 0; i <10; i + +) {
a [i] = 2 * i;
System.out.println (a [i]);
}

}
public static void sayOK ()

{

System.out.println ("OK");

}

public static String getHello ()

{
String str1 = "Hello";
return str1;
}
public static String removeHtml (String str) {
String strText = null;
strText = str.replaceAll ("<(?:. | \ \ s) *?>", "");
return strText;
}
public static void ArrayDemo (int array [])
{
int a [] = null;
a = new int [10];
for (int i = 0; i <10; i + +) {
a [i] = 2 * i;
System.out.println (a [i]);
}
}
}
jsp page calls
<% @ page import = "javabean.HelloWord"%>
<%
String strText = " java ";
out.print (HelloWord.removeHtml (strText));
out.print (HelloWord.getHello ());
out.print (HelloWord.main ());
/ / out.print (HelloWord.ArrayDemo (int array [])); error
%>
access jsp files can be printed normally . I am asking is how correct print ArrayDemo () method , as well as how to call directly on the main () method to print out . Beginner, forgive me if wrong place description :)
------ Solution ---------------------------- ----------------
out.print (HelloWord.ArrayDemo (new int [] {1, 2, 3}));

------ Solution ------------------------------------ --------
when the class name and the file name is known , java xx run will directly call the main method .
main method must have the format
public static void main (String [] args) {
}
------ Solution ----------------------------------- ---------
/ / out.print (HelloWord.ArrayDemo (int array [])); error
this error is that you do not understand what is the class and instance methods , ArrayDemo HelloWord class is an instance method , called an instance method is called when an object must be new to call this method , but not the same class method (static modification, the class name when called directly . method name ) .
<%
String strText = " java ";
HelloWord hw = new HelloWord (); out.print (HelloWord.removeHtml (strText));
out.print (HelloWord.getHello ());
out.print (HelloWord.main ());
/ / out.print ( hw.ArrayDemo (int array [])); Normal %>

------ Solution ------------------------------------ --------
sorry wrong, sorry ah !
haha laughed !

I know the reason why you are wrong
you tune ArrayDemo (int array []) when the parameters of how can this happen ?
must be initialized , otherwise how inside the algorithm execution method
int [] arr = [1,2,3,4,5];
out.print (HelloWord.ArrayDemo (arr) ;/ / this fishes

According to your ideas but do not participate directly in the pass local variables defined inside the method , you HelloWord inside the class method ArrayDemo parameters can be deleted.
way out on the line

out.print (HelloWord.ArrayDemo () ;/ / also
------ Solution ---------------------- ----------------------
first, you call the static method ArrayDemo no incoming parameters, modify the following

int array[] = {1,2,3}; //定义传入参数
out.print(HelloWord.ArrayDemo(array);

Second, you define a method ArrayDemo the int array parameter is simply not used, it should be int a [] it

public static void ArrayDemo(int array[])
{
int a[]=null;
a = new int[10];
for(int i=0;i<10;i++){
a[i] = 2*i;
System.out.println(a[i]);
}
}

In addition, the method is named the best start with a lowercase letter

identification is completed

------ For reference only ---------------------------------- -----
int array [] = {1,2,3}; / / define the incoming parameters
out.print (HelloWord.ArrayDemo (array);
--------------
modified so still being given ah.
Also you modify the way ArrayDemo me what is the difference ?
public static void ArrayDemo (int array [])
{
int a [] = null;
a = new int [10];
for (int i = 0; i <10; i + +) {
a [i] = 2 * i;
System.out.println (a [i]);
}
}
------ For reference only --------------------------------- ------
I have encountered such a problem ah , the landlord can tell me that you were the solution?

没有评论:

发表评论