在编译我的程序时,我收到了这个错误。运算符&Quot;+&Quot;不能应用于&Quot;System.Random&Quot;和&Quot;System.Random&Quot;的操作数 [英] I am getting this error when compiling my program. Operator " + " cannot be applied to operands of "System.Random" and "System.Random"

查看:0
本文介绍了在编译我的程序时,我收到了这个错误。运算符&Quot;+&Quot;不能应用于&Quot;System.Random&Quot;和&Quot;System.Random&Quot;的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为数学问题生成随机数。编译程序时,错误运算符"+"不能应用于类型为"System.Random"和"System.Random"的操作数。我真的不知道如何修复此错误。

            Random num1 = new Random(0);                
            Random num2 =new Random(0); 
            int rand; 


            Console.Write("What is");
            Console.Write(num1);
            Console.Write( " - ");
            Console.Write( num2);
            Console.Write( "?");
            int answer = Console.Read();

                    if (num1 + num2 == answer) ERROR {
                    Console.Write(" Your are Correct!
");
                    correctCount++;

                }
                    else
                    Console.Write( "Your answer is wrong" );
                    Console.Write(num1);
                    Console.Write(" + ");
                    Console.Write(num2);
                    Console.Write("should be ");
                    Console.Write(num1 + num2); ERROR
                    count++;
                }
            }

            }
        }
现在我可以编译该程序而不会出现任何错误,但它不会生成任何数字。有没有人看到我做错了什么。

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

               using Android.App;
               using Android.Content;
               using Android.OS;
               using Android.Runtime;
               using Android.Views;
               using Android.Widget;
             namespace Jagtutor
             {

[Activity(Label = "Addition")]
public class AdditionsActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Create your application here
        SetContentView(Resource.Layout.Second);

        var label = FindViewById<TextView>(Resource.Id.screen3Label);
        label.Text = Intent.GetStringExtra("ModuleData") ?? "Data not available";

        {
        int correctCount = 0;
        int count = 0;

             while (count < 10);
            {
                Random gen = new Random();
                int num1 = gen.Next();
                int num2 = gen.Next(); 


            Console.Write("What is");
            Console.Write(num1);
            Console.Write( " - ");
            Console.Write( num2);
            Console.Write( "?");
            int answer = Int32.Parse(Console.ReadLine());

                    if (num1 + num2 == answer) {
                    Console.Write(" Your are Correct!
");
                    correctCount++;

                }
                    else
                    Console.Write( "Your answer is wrong" );
                    Console.Write(num1);
                    Console.Write(" + ");
                    Console.Write(num2);
                    Console.Write("should be ");
                    Console.Write(num1 + num2);
                    count++;
                }
            }

            }
        }

}

推荐答案

您误解了Random的使用方法:它不是数字,它是一个可用于生成随机数字的类。

试试:

// Make a generator
Random gen = new Random();
// Now we can use our generator to make new random numbers like this:
int num1 = gen.Next();       
int num2 = gen.Next();       
您每次调用gen.Next()时都会得到一个新的随机数。如果希望随机数序列是可重复的,请将一个数字(任何数字)传递给Random的构造函数。请注意,每次运行程序时,结果都将保持不变。

这篇关于在编译我的程序时,我收到了这个错误。运算符&Quot;+&Quot;不能应用于&Quot;System.Random&Quot;和&Quot;System.Random&Quot;的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆