我试图为数学问题生成随机数。程序编译时,错误的操作符“+”不能应用于'System.Random'和'System.Random'类型的操作数。我真的不知道如何解决这个错误。编译我的程序时出现此错误。运算符“+”不能应用于“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++;
}
}
}
}
}
除了回答您的实际问题(其错误消息已经告诉你挺清楚),'Console.Read'不会做你认为它的作用:http://msdn.microsoft.com/en -us /库/ system.console.read.aspx。你可能想要:'int answer = Int32.Parse(Console.ReadLine())'。 – 2012-04-25 18:13:39