gpt4 book ai didi

c# - 编译我的程序时出现此错误。运算符 "+ "不能应用于 "System.Random"和 "System.Random"的操作数

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:28 25 4
gpt4 key购买 nike

我正在尝试为数学问题生成随机数。编译程序时,错误运算符“+”不能应用于“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!\n");
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!\n");
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 的构造函数。请注意,每次运行程序时,结果都将保持不变。

关于c# - 编译我的程序时出现此错误。运算符 "+ "不能应用于 "System.Random"和 "System.Random"的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10321246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com