【问题标题】:C# User Input Random numbers [closed]C#用户输入随机数[关闭]
【发布时间】:2013-01-31 22:11:21
【问题描述】:

我需要帮助编写一个代码,让用户在不重复相同数字的情况下输入 50 到 150 之间的 10 个数字。这将是一个循环程序。到目前为止,这是我所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IP3_Program
{
    class Program
    {
        static void Main()
        {
            int total = 0;
            string inValue;
            int [] number = new int[10];
            for (int i = 0; i < number.Length; i++)
            {
                Console.Write("Enter number{0}: ", i + 1);
                inValue = Console.ReadLine();
                number[i] = Convert.ToInt32(inValue);
            }              
        }
    }
}

【问题讨论】:

  • 目前看起来还不错。您需要什么帮助?
  • 你想看if
  • 您需要有一个特定的问题,以便我们可以帮助您,而不仅仅是要求完美运行的代码。

标签: c# loops if-statement random input


【解决方案1】:
var numbers = new List<int>();
while (numbers.Count != 10)
{
    // get number from user

    if (numbers.Contains(newNumber) || newNumber < 50 || newNumber > 150)
    {
        // reject the number
        // you'd probably want to display a message here
        // alerting the user another input in needed
    }
    else
    {
        numbers.Add(newNumber);
    }
}

【讨论】:

  • 需要在50150之间
  • @Default 10 个介于 50 和 150 之间的数字
  • 完全正确。那么您的代码如何验证49
  • 几乎,翻转条件 - 如果它在 50 到 150 之间,请不要拒绝。
  • 哎呀,谢谢...这总是让我...
猜你喜欢
  • 2019-08-05
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多