就是写段完整程序,用到两个 static 的 method(除main()之外)
class SomeClass{
static void m1(){//somecode}
static void m2() {//somecode}
m3(){}
public static void main(String[] args)
{//somecode}差不多吧
class SomeClass{
static void m1(){//somecode}
static void m2() {//somecode}
m3(){}
public static void main(String[] args)
{//somecode}差不多吧
我手头没有材料。
另外,static mathod 和 main method 区别在哪里?谢谢
main method就是一个static 方法,因为main是java程序的入口函数,所以必须使用static方法。详细的例子很简单:public class Test
{
public void static method1(String str){
System.out.println("This is a test of method 1"+s);
} public void static method2(){
System.out.println("This is a test of method 2");
} public void method3(String str){//非static 方法
System.out.println("This is a test of method 3"+s);
}
public static void main(String[] args)
{
Test t = new Test();
Test.method1("hello!");
Test.method2();
t.method1("non static method");
}
}
main method在执行是回被首先调用
public class Test
{
public static void method1(String str){
System.out.println("This is a test of method 1"+str);
} public static void method2(){
System.out.println("This is a test of method 2");
} public void method3(String str){//非static 方法
System.out.println("This is a test of method 3"+str);
}
public static void main(String[] args)
{
Test t = new Test();
Test.method1("hello!");
Test.method2();
t.method1("non static method");
}
}
public static int readInt (String prompt){
String inputString = JOptionPane.showInputDialog(prompt);
return Integer.parseInt (inputString);
}
//output method
public static void output (String prompt){
JOptionPane.showMessageDialog (null, prompt);
System.out.println (prompt);
}
public static void main (String[] args){ //variables here int stuNum;
int num;
int tryAgain;
int numberCount;
final int LENGTH = 7;
boolean keepGoing = true; while (keepGoing) {
numberCount = 0;
stuNum = readInt("Input your student #");
num = readInt ("Input a digit from 1 - 9"); for (int i = 0; i<LENGTH; i++){
if (stuNum % 10 == num)
numberCount ++;
stuNum = stuNum / 10; }
//close for loop output("The number " + num +
" appears " + numberCount + " time(s) in your student number"); tryAgain = readInt("Do you want to try again? (0 to quit)");
if (tryAgain == 0)
keepGoing = false; } //close while loop output ("Good-bye"); System.exit (0);
}
}