怎么在主函数里调用后面的函数?JAVA
mportjava.util.Scanner;publicclassCH06_01{publicstaticvoidmain(String[]args){Scanneri...
mport java.util.Scanner;
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
System.out.println("最大公约数为"+b);
}
int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;}
return b 展开
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
System.out.println("最大公约数为"+b);
}
int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;}
return b 展开
3个回答
展开全部
import java.util.Scanner;
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
System.out.println("最大公约数为"+b);
System.out.println(gcd(a,b));
}
public static int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
}
注意编写代码的格式,如果在同一个类中在主函数里调用方法,方法必须为static修饰,原因很简单,main方法为类方法,类方法只能调用类变量和类方法。如果是多个类就创建对象,然后通过对象来调用就行…………
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
System.out.println("最大公约数为"+b);
System.out.println(gcd(a,b));
}
public static int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
}
注意编写代码的格式,如果在同一个类中在主函数里调用方法,方法必须为static修饰,原因很简单,main方法为类方法,类方法只能调用类变量和类方法。如果是多个类就创建对象,然后通过对象来调用就行…………
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你只需要使用类名.方法名就可以了
public static void main(String[]args){
CH06_01 ch=new CH06_01();
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
ch.gcd(a,b);
System.out.println("最大公约数为"+b);
}
public static void main(String[]args){
CH06_01 ch=new CH06_01();
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int a =input.nextInt();
System.out.println("please enter the second number");
int b =input.nextInt();
ch.gcd(a,b);
System.out.println("最大公约数为"+b);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一下是源代码:(注意楼主,主函数中不鞥用a,b,这里用m,n代替)
import java.util.Scanner;
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int m =input.nextInt();
System.out.println("please enter the second number");
int n =input.nextInt();
System.out.println("最大公约数为"+b);
CH06_01 p=new CH06_01();
p.gcd(m,n);
}
int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;}
return b;
}
import java.util.Scanner;
public class CH06_01 {
public static void main(String[]args){
Scanner input=new Scanner(System.in);
System.out.println("please enter the first number");
int m =input.nextInt();
System.out.println("please enter the second number");
int n =input.nextInt();
System.out.println("最大公约数为"+b);
CH06_01 p=new CH06_01();
p.gcd(m,n);
}
int gcd(int a,int b){
int r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;}
return b;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询