【问题标题】:Generate 4 random number whose sum is 100 and one is more than 50生成 4 个随机数,总和为 100,其中一个大于 50
【发布时间】:2014-01-16 21:47:07
【问题描述】:

我需要创建 4 个总和为 100 的 int 随机数。其中一个大于 50 并且比其他的大。 我有这个:

int a=0, b=0,c=0,d=0;
int cem=100;
while (a+b+c+d=cem){
Random perc = new Random();
a = perc.Next(50, 100);
b = perc.Next(0, 50);
c = perc.Next(0, 50);
d = perc.Next(0, 50);
}

在编译器中我得到 2 个错误:

赋值的左边必须是变量,索引器的属性 无法将类型“int”隐式转换为“bool”

【问题讨论】:

  • a+b+c+d=cem 更改为a+b+c+d==cem 将解决编译器错误。就目前而言,我看不到您的 while 循环的内容将如何运行。

标签: c#


【解决方案1】:

替换

while (a+b+c+d=cem){

while (a+b+c+d!=cem){

您使用的是赋值 (=) 而不是比较 (==/!=)。

【讨论】:

  • 其实应该是!=,否则为假,循环不会运行。
  • 非常真实! @Pierre-LucPineault 谢谢!
【解决方案2】:

除了有关编译器错误消息的其他答案外,您还应该移动该行

Random perc = new Random();

while 循环的外部。您只需要一个随机数生成器,并且由于时间种子的原因,在快速循环中重新创建它可能会产生相同的结果。

【讨论】:

    【解决方案3】:

    如果你想一想,四个总和为 100 的随机数意味着其中只有三个是随机的,第四个是 100 减去其他三个......所以不要做一个循环,先生成一个数字,然后再生成另一个剩下的间隔,然后是第三个。

    【讨论】:

      【解决方案4】:

      为什么要使用循环?祝你得到你想要的东西:-)

      (浪费了这么多cpu)

      这是我将如何开始做的;

      class Program
      {
          static void Main(string[] args)
          {
              int a = 0, b = 0, c = 0, d = 0;
              int cem = 100;
              Random perc = new Random();
      
              a = perc.Next(50, cem);
              cem -= a;
      
              b = perc.Next(0, cem);
              cem -= b;
      
              c = perc.Next(0, cem);
              cem -= c;
      
              d = cem;
      
              Console.WriteLine("{0} + {1} + {2} + {3} = {4}",a,b,c,d,a+b+c+d);
      
              Console.ReadKey(false);
          }
      }
      

      【讨论】:

        【解决方案5】:
        The left-hand side of an assignment must be a variable
        Cannot implicitly convert type 'int' to 'bool'
        

        while 想要 ==,假设 C# 就像 C。== 是相等测试,= 是赋值。

        (应该很明显为什么这会导致第一个错误消息。您可能需要考虑为什么它解释了第二个,但由于这样做是一个很好的练习,我不打算解释。)

        【讨论】:

          【解决方案6】:

          这样的事情怎么样,循环的数量会更少吗?

          int a = 0, b = 0, c = 0, d = 0;
          int cem = 100;
          Random perc = new Random();
          a = perc.Next(50, cem);
          b = perc.Next(0, cem - a);
          c = perc.Next(0, cem - a - b);
          d = cem - a - b - c;
          

          【讨论】:

            【解决方案7】:
            class Program {
                void Main() {
                    var random = new Random();
            
                    // note it says one of them is more than 50
                    // so the min value should be 51 not 50
                    var a = random.Next(51, 100);
            
                    // the rest of the number will be less than `a` 
                    // because `a` is more than 50 so the max `remaining` 
                    // will be is 49 (100 - 51)
                    var remaining = 100 - a; 
            
                    var b = random.Next(0, remaining);
                    remaining -= b;
            
                    var c = random.Next(0, remaining);
                    remaining -= c;
            
                    var d = remaining;
            
                    Console.WriteLine("a: " + a);
                    Console.WriteLine("b: " + b);
                    Console.WriteLine("c: " + c);
                    Console.WriteLine("d: " + d);
                    Console.WriteLine("total: " + (a + b + c + d));
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-06-29
              • 2014-12-27
              • 1970-01-01
              • 1970-01-01
              • 2016-10-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多