感谢您的输入,非常感谢!
到目前为止,我已经发布了完整的代码,它现在给了我看起来像“随机”的数字,因为当我运行它时它们总是不同的
@Steve,谢谢...我已经查看了这些问题,但是所有解决方案都涉及使用我不允许使用的 random() 以外的其他技术
@Oerkelens 谢谢,当我将 random() 的代码移到循环之外时,我得到了两个可能的结果,一个是一系列 9 位、9 位随机数,或者一个异常说明
错误 1 不能在此范围内声明名为“randomNumber”的局部变量,因为它会给“randomNumber”赋予不同的含义,后者已在“父级或当前”范围内用于表示其他内容
我已经发布了更大的代码来显示我为使其工作所做的更改...我真的不明白如何从循环中正确调用 random() ,但由于某种原因,具有相同的行循环内部和外部都成功了
@Preston - 我们没有这门课程的教科书,我们只能使用 Bob Tabor (learnvisualstudiodotnet) 和 Envato (30 天内学习 C#) 的 Microsoft C# 视频教程中包含的技术
如果这一切对您来说似乎很明显,我深表歉意,但是在课程进行到一半时,我们被告知我们正在从学习 Visual Basic 编程切换到 C#,所以我们现在所有的工作都需要重新- 用 C# 编写,没有任何关于如何使用这种语言的特定说明......不用说,这是一个巨大的压力,我们没有任何资源来做这件事,所以我们所做的大部分工作都是猜测
“工作”的更完整的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GuessingGameProgram
{
class Program
{
int randNum;
static void Main(string[] args)
{
// Create a string array that consists of ten lines.
string[] personalNumbers; // declare personalNumbers as a 10-element array
personalNumbers = new string[10]; //= { "First number", "Second number", "Third line", etc}
Random outsideLoopRandom = new Random();
int randomNumber = outsideLoopRandom.Next(1, 50);
for (int i = 0; i < 9; i++) // populate the array with 10 random values
{
randomNumber = outsideLoopRandom.Next(1, 50);
string RandomNumberText = Convert.ToString(randomNumber);
personalNumbers[i] = RandomNumberText;
}
// WriteAllLines creates a file, writes a collection of strings to the file,
// and then closes the file.
//System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
foreach (string i in personalNumbers) // this is just a test to see what the output is
{
Console.Write("{0} ", i);
}
Console.ReadLine();
}
}
}
//randNum = Random.Equals(1, 50);
//StreamReader myReader = new StreamReader("personalNumbers.txt");
//string line = "";
//while (line != null)
//{
// line = myReader.ReadLine();
// if (line != null)
// Console.WriteLine(line);
//}
//myReader.Close();
//Console.ReadLine();
//personalNumbers = RandomNumbers.next(1, 10);
//int returnValue = personalNumbers.Next(1, 50);
//int Guess = 0;
//Console.WriteLine("Please guess a number between 1 and 50");
//Console.ReadLine();
////while (Guess = Convert.ToInt32(Console.Read());
//if (Guess < returnValue)
//{
// Console.WriteLine("Wrong! the number that I am thinking of is higher than " + Guess + ". Try again!");
// Console.ReadLine();
//}
//if (Guess > returnValue)
//{
// Console.WriteLine("Wrong! The number that I am thinking of is lower than " + Guess + ". Try again!");
// Console.ReadLine();
//}
// else if (Guess = returnValue)
// Console.WriteLine("Correct! The number that I was thinking of was " + Guess + ". Congratulations!");
// //{
//Console.WriteLine("Let's play a guessing game!")
//Console.WriteLine("")
//Console.WriteLine("guess a number between 1 and 10")
//Console.WriteLine("")
//randNum = randomGenerator.Next(1, 10)
//While userGuess <> randNum
// {
// userGuess = Console.ReadLine()
// }
// If userGuess > randNum Then
// Console.WriteLine("too high, guess again!")
// {
// If userGuess < randNum Then
// Console.WriteLine("too low, guess again!")
// }
// Else
//End While
//Console.WriteLine("Correct! the secret number is " & randNum)
//Console.ReadLine()