标题:[求助]写程序碰到个问题
取消只看楼主
hjl2006404
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-10-4
结帖率:50%
 问题点数:0 回复次数:2 
[求助]写程序碰到个问题
想写个简单的c#程序,该程序实现的是由用户提供数字,求其平均数或总数,可以选择求哪种,但在用户选择出错的时候可以实现请求再输入,下面是我写的程序,但是不能实现在输入功能,求帮助!!
using System;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入你想要得数组的个数:");
        int num = int.Parse(Console.ReadLine());
        int[] A = new int[num];
        Console.WriteLine("请输入你想要计算的数字(整数)");
        for (int i = 0; i < num; i++)
        {
            A[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("请选择你所要进行的操作\n" + "+表示求和\n" + "/表示平均数");
        char operate = Convert.ToChar(Console.ReadLine());
        do
        {
            switch (operate)
            {
                case '+':
                    {
                        int all = 0;
                        for (int i = 0; i < num; i++)
                        {
                            all += A[i];
                        }
                        Console.WriteLine("所求数字之和为:{0}", all);
                        break;
                    }
                case '/':
                    {
                        int all = 0;
                        for (int i = 0; i < num; i++)
                        {
                            all += A[i];
                        }
                        Console.WriteLine("所有数字的平均数为:{0}", all / num);
                        break;
                    }
                default:
                    {
                        break;
                    }

            }
            Console.WriteLine("请重新选择操作");
        } while (operate != '+' || operate != '/');
        Console.ReadKey();
    }
}
搜索更多相关主题的帖子: class 平均数 用户 
2010-10-05 01:13
hjl2006404
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-10-4
得分:0 
终于将问题给解决了,知道怎么整这个问题了,修改后的代码:
using System;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("请输入你想要得数组的个数:");
        int num = int.Parse(Console.ReadLine());
        int[] A = new int[num];
        Console.WriteLine("请输入你想要计算的数字(整数)");
        for (int i = 0; i < num; i++)
        {
            A[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("你所想要计算的数字是这些:");
        foreach (int s in A)
        {
            Console.Write(s);
        }
        Lab:
        Console.WriteLine("\n请选择你所要进行的操作\n" + "+表示求和\n" + "/表示平均数");
        char operate = Convert.ToChar(Console.ReadLine());
        if (operate == '+' || operate == '/')
        {
            switch (operate)
            {
                case '+':
                    {
                        int all = 0;
                        for (int i = 0; i < num; i++)
                        {
                            all += A[i];
                        }
                        Console.WriteLine("所求数字之和为:{0}", all);
                        break;
                    }
                case '/':
                    {
                        int all = 0;
                        for (int i = 0; i < num; i++)
                        {
                            all += A[i];
                        }
                        Console.WriteLine("所有数字的平均数为:{0}", all / num);
                        break;
                    }
                default:
                    {
                        break;
                    }

            }
        }
        else
        {
            Console.WriteLine("请再次选择你所要进行的操作");
                goto Lab;
        }
        
       Console.ReadKey();
    }
}
2010-10-05 08:25
hjl2006404
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2010-10-4
得分:0 
我原先也是打算用while循环来实现的,但是使用时达不到标准,所以我改用了goto语句...
2010-10-08 10:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-322004-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.161331 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved