Topic: 求最大公约数和最小公倍数。。。为什么出错了

  Print this page

1.求最大公约数和最小公倍数。。。为什么出错了 Copy to clipboard
Posted by: 黑珍珠
Posted on: 2006-06-06 20:11


import java.io.*;
public class Number
{
public static void main(String args[])
{
int num1,num2,num3,N,M;
String s="";
System.out.print("请输入第一个数");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s=in.Runnable();
}catch(IOException e){}
num1=integer.parseInt(s);
System.out.print(请输入第二个数);
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}catch(IOException e){}
num2=integer.parseInt(s);
}
System.out.print(请输入第三个数);
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}catch(IOException e){}
num3=integer.parseInt(s);
}

/*求两个数的最大公因数*/
int a = num1 ,b = num2 ,c = 1;
while(c!=0)
{
c=a%b;
if(c!=0)
{
a=b;
b=c;
}
}
N=b;
a=N;b=num3;c=1;
while(c!=0)
{
c=a%b;
if(c!=0)
{
a=b;
b=c;
}
}
N=b;
a=M;b=num3;c=1;
while(c!=0)
{
c=a%b;
if(c!=0)
{
a=b;
b=c;
}
}
M=M*num3/b;
System.out.println("这个数的最大公约数: "+N);
System.out.println("这个数的最小公倍数"+M);
}

怎么出错了。为什么啊 有人能帮忙看看嘛


{ use [code ] tag and check "Disable Smileys" -- why

2.Re:求最大公约数和最小公倍数。。。为什么出错了 [Re: 黑珍珠] Copy to clipboard
Posted by: why
Posted on: 2006-06-06 21:18

I only corrected the syntax errors.
Please be more careful when typing.

import java.io.*;

public class Number {
public static void main(String args[]) {
int num1, num2, num3, N = 0, M = 0; // initialize M & N
String s = "";
System.out.print("请输入第一个数");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine(); // s=in.Runnable();
} catch (IOException e) {}
num1 = Integer.parseInt(s); // Integer
System.out.print("请输入第二个数"); //quotation marks
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch (IOException e) {}
num2 = Integer.parseInt(s); // Integer
// }
System.out.print("请输入第三个数"); //quotation marks
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch (IOException e) {}
num3 = Integer.parseInt(s); // Integer
// }

/* 求两个数的最大公因数 */
int a = num1, b = num2, c = 1;
while (c != 0) {
c = a % b;
if (c != 0) {
a = b;
b = c;
}
}
N = b;
a = N;
b = num3;
c = 1;
while (c != 0) {
c = a % b;
if (c != 0) {
a = b;
b = c;
}
}
N = b;
a = M;
b = num3;
c = 1;
while (c != 0) {
c = a % b;
if (c != 0) {
a = b;
b = c;
}
}
M = M * num3 / b;
System.out.println("这个数的最大公约数: " + N);
System.out.println("这个数的最小公倍数" + M);
}
} // added

3.Re:求最大公约数和最小公倍数。。。为什么出错了 [Re: 黑珍珠] Copy to clipboard
Posted by: Skybus
Posted on: 2006-06-06 21:47

下面这个程序不知能不能帮你,可以对照一下
import java.io.*;

public class NumberTest {

  public static void main(String args[]) {

    long M, N;
    String s = "";
    System.out.print("请输入第一个数");
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(
          System.in));
      s = in.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }
    M = Integer.parseInt(s);
    System.out.print("请输入第二个数");
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(
          System.in));
      s = in.readLine();
    } catch (IOException e) {
      e.printStackTrace();
    }
    N = Integer.parseInt(s);

    NumberCal(M, N);
  }

  static int NumberCal(long M, long N) {
    long T, P, Q = 1;// P表示最大公约数,Q表示最小公倍数
    P = Q;
    if (M == N) {
      P = Q = M;
      System.out.println("M,N的最大公约数: " + P);
      System.out.println("M,N的最小公倍数: " + Q);
      return 1;
    }
    if (M < N) {
      T = M;
      M = N;
      N = T;
    }
    long M1 = M;
    Long N1 = N;
    while (true) {
      T = M1 % N1;
      if (T == 0) {
        P = N1;
        Q = M * N / P;
        break;
      }
      if (T == 1) {
        P = 1;
        Q = M * N;
        break;
      }
      M1 = N1;
      N1 = T;
    }
    System.out.println("M,N的最大公约数: " + P);
    System.out.println("M,N的最小公倍数: " + Q);
    return 1;
  }
}

4.Re:求最大公约数和最小公倍数。。。为什么出错了 [Re: 黑珍珠] Copy to clipboard
Posted by: 黑珍珠
Posted on: 2006-06-06 22:26

think you谢谢
太粗心了
大小写都搞错了

5.Re:求最大公约数和最小公倍数。。。为什么出错了 [Re: 黑珍珠] Copy to clipboard
Posted by: cainiao025
Posted on: 2006-06-09 01:15

[color=black]
太复杂了。
class Num
{
public static void main(String args[])
{
int m=Integer.parseInt(args[0]);
int n=Integer.parseInt(args[1]);
int total, r;
total=m*n;
do
{
if(m<n)
{
int t=m;
m=n;
n=t;
}

r=m%n;
m=n;
n=r;
}while(r!=0):
System.out.println("最大公因数是:"+m);
System.out.println("最小公倍数是:"+total/m);
}
}
[/color]


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923