我正在尝试为数学问题生成随机数。编译程序时,错误运算符"+"不能应用于类型为"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
的构造函数。请注意,每次运行程序时,结果都将保持不变。
上一篇:播放YouTube视频窗口的形式形式、窗口、视频、YouTube
下一篇:自动连接BuildProperties Spring Boot 2.1.5失败&//eclipseBoot、Spring、BuildProperties、eclipse