当前位置: 代码迷 >> Java相关 >> 几道编程题不会,大家看看要怎么做?
  详细解决方案

几道编程题不会,大家看看要怎么做?

热度:730   发布时间:2006-04-28 16:51:00.0

别在IF语句直接返回RETURN的值就可以了
可是为什么总是返回1呢,呵呵
再研究………………

[此贴子已经被作者于2006-4-28 16:58:08编辑过]


----------------解决方案--------------------------------------------------------

哈哈,成功了!!!
import java.io.*;
class GreatestCommonDivisor
{
public int getcd(int a,int b)
{
int i,j;
int m=1;
j=(a>b)?b:a;

for(i=j;i>0 ;i--)

if(a%i==0&&b%i==0)
{
break;
}

return i;// 缺少返回语句。

}
public static void main(String args[])
{
try
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=new String();
String str1=new String();
System.out.println("Enter two numbers:");
str=br.readLine();
str1=br.readLine();
int m=Integer.parseInt(str);
int n=Integer.parseInt(str1);
GreatestCommonDivisor cd=new GreatestCommonDivisor();
int e=cd.getcd(m,n);
System.out.println("The Greatest Common Divisor is: "+e);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


----------------解决方案--------------------------------------------------------
晚上八点的火车要回家了,呵呵
下班了
----------------解决方案--------------------------------------------------------

最终修订版,呵呵
import java.io.*;
class GreatestCommonDivisor
{
public int getcd(int a,int b)
{
int i,j;
j=(a>b)?b:a;
for(i=j;i>0 ;i--)
if(a%i==0&&b%i==0)
{
break;
}
return i;// 缺少返回语句。
}
public static void main(String args[])
{
try
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=new String();
String str1=new String();
System.out.println("Enter two numbers:");
str=br.readLine();
str1=br.readLine();
int m=Integer.parseInt(str);
int n=Integer.parseInt(str1);
GreatestCommonDivisor cd=new GreatestCommonDivisor();
int e=cd.getcd(m,n);
System.out.println("The Greatest Common Divisor is: "+e);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


----------------解决方案--------------------------------------------------------
听海★蓝心梦
做的相当好 好好努力!
[IMG]http://www1.myelogo.com/elogo_user/31356.jpg[/IMG]

----------------解决方案--------------------------------------------------------
楼上的高手是不是传说中的黑客啊,我要拜你为师啊
你太厉害啊,我五体投地啊

你的QQ是多少啊?
----------------解决方案--------------------------------------------------------

呵呵 什么黑客啊
就是闹着玩的
和我讨论java可以
哈哈 我qq 396651058[IMG]http://www1.myelogo.com/elogo_user/31573.jpg[/IMG]


[此贴子已经被作者于2006-4-28 23:25:52编辑过]


----------------解决方案--------------------------------------------------------
像这种基础题一般什么书上有呢?
----------------解决方案--------------------------------------------------------
第四题要怎么做呀?
----------------解决方案--------------------------------------------------------
呵呵,各位高手要好好帮忙啊
----------------解决方案--------------------------------------------------------
  相关解决方案