class Program
    {
        static void Main(string[] args)
        {
            unit a, b;
            Console.WriteLine("请输入第一个正整数:");
            while ( !unit.TryParse(Console.Read Line() ,out a));
            Console.WriteLine("格式不正确,请重新输入:");
            Console.WriteLine("请输入第二个正整数:");
            while(!uint.TryParse(Console.ReadLine(),out b))
                Console.WriteLine("格式不正确,请重新输入:");
            for( uint  i = a>b?a:b; i<=a*b ; i++)
            {
                if ( i % a ==0 && i % b == 0)
                {
                    Console.WriteLine("最小公倍数为:" +i);
                    break ;
                }
            }
        }
    }
}
要求是在主方法中由用户输入两个正整数 并且计算最小公倍数  如果还要在程序中加入计算最大公约数的代码  定义i 为a 和b 之间的较小值 以i为循环 怎么写这个程序

解决方案 »

  1.   

    两个功能分别封装到不同的函数,main中调用
      

  2.   

    1. a,b的定义有问题,应该是 uint,而不是unit
    2.while (!uint.TryParse(Console.ReadLine(), out a)) 后面的分号去掉,和下面的为一整体
    3.是否加入了命名空间  using System.Collections.Generic; 这个的引用
    正确如下:public static void Main()
            {            uint a, b;
                Console.WriteLine("请输入第一个正整数:");
                while (!uint.TryParse(Console.ReadLine(), out a))
                    Console.WriteLine("格式不正确,请重新输入:");
                Console.WriteLine("请输入第二个正整数:");
                while (!uint.TryParse(Console.ReadLine(), out b))
                    Console.WriteLine("格式不正确,请重新输入:");
                for (uint i = a > b ? a : b; i <= a * b; i++)
                {
                    if (i % a == 0 && i % b == 0)
                    {
                        Console.WriteLine("最小公倍数为:" + i);
                        break;
                    }
                }
                for (uint j = a > b ? b : a; j > 0; j--)
                {
                    if (a % j == 0 && b % j == 0)
                    {
                        Console.WriteLine("最大公约数为:" + j);
                        break;
                    }
                }
                Console.Read();        }
      

  3.   

    怎么有问了一遍啊, for (uint j = a > b ? b : a; j > (a > b ?a : b)-1; j--)
                {
                    if (a % j == 0 && b % j == 0)
                    {
                        Console.WriteLine("最大公约数为:" + j);
                        break;
                    }
                }
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int a, b,min,sumnu;
                int max = 1;            a = int.Parse(textBox1.Text);            b = int.Parse(textBox2.Text);            if (a>b) 
                {
                    min=b;
                }
                else
                {
                    min=a;
                }            for (int i = 1; i <= min; i++)
                {
                    if ((a%i==0) && (b%i==0))
                    {
                        max = i;
                    }
                }            sumnu = (a * b )/max;   //最大公约数            this.label1.Text = sumnu.ToString();  
            }
        }
    }