[code=Java]
[class A
{int x,y,z;
A(int a,int b)
{
x=a;
y=b;
}
int getMax()
{
if(x<y)
{
int e=y;
y=x;
x=e;
}
do{
z=x%y;
x=y;
y=z;
}while(z!=0);
return x;
}
}
class B
{
}
class C
{
public static void main(String [] args)
{
A s=new A(10,5);
s.getMax();
System.out.println(s.getMax());
}
}/code]
[class A
{int x,y,z;
A(int a,int b)
{
x=a;
y=b;
}
int getMax()
{
if(x<y)
{
int e=y;
y=x;
x=e;
}
do{
z=x%y;
x=y;
y=z;
}while(z!=0);
return x;
}
}
class B
{
}
class C
{
public static void main(String [] args)
{
A s=new A(10,5);
s.getMax();
System.out.println(s.getMax());
}
}/code]
你在System.out.println(s.getMax())这一句第二次调用时候,会出现除0的异常
class A {
int x,y,z=10000;
A(int a,int b) {
x=a;
y=b;
}
int getMax() {
if(x <y) {
int e=y;
y=x;
x=e;
}
while(z!=0){
z = x % y;
x = y;
y = z;
}
return x;
}
}
class B{ }
class C {
public static void main(String [] args) {
A s=new A(300,99);
s.getMax();
System.out.println(s.getMax());
}
}
int x,y,z=10000;
A(int a,int b) {
x=a;
y=b;
}
int getMax() {
if(x <y) {
int e=y;
y=x;
x=e;
}
while(z!=0){
z = x % y;
x = y;
y = z;
}
return x;
}
}
class B{ }
class C {
public static void main(String [] args) {
A s=new A(300,99);
s.getMax();
System.out.println(s.getMax());
}
}
还有一种 public static void main(String str[]){
getMaxCom(-20);
}
public static int getMaxCom(int c){
int b = Math.abs(c);
if(b<=2){
System.out.println("没有最大公约数");
}
for(int i =2;i<b;i++){
if(0==b%i){
System.out.println("最大公约数=="+b/i);
return b/i;
}
}
return 0;
}